• POJ1251(Kruskal水题)


    https://vjudge.net/problem/POJ-1251


    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

    Input

    The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

    Output

    The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

    Sample Input

    9
    A 2 B 12 I 25
    B 3 C 10 H 40 I 8
    C 2 D 18 G 55
    D 1 E 44
    E 2 F 60 G 38
    F 0
    G 1 H 35
    H 1 I 35
    3
    A 2 B 10 C 40
    B 1 C 20
    0

    Sample Output

    216
    30
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<cmath>
     6 #define maxn 30
     7 #define ms(x,n) memset(x,n,sizeof x);
     8 const int inf=0x3f3f3f3f;
     9 using namespace std;
    10 int parent[maxn],ranks[maxn];
    11 int V,E;
    12 int finds(int x)
    13 {
    14     if(x!=parent[x])
    15         return parent[x]=finds(parent[x]);
    16 }
    17 void init(int n)
    18 {
    19     for(int i=0;i<n;i++)
    20         parent[i]=i,ranks[i]=0;
    21 }
    22 void unite(int x,int y)
    23 {
    24     x=finds(x),y=finds(y);
    25     if(x==y)return;
    26     if(ranks[x]>ranks[y])
    27         parent[y]=x;
    28     else
    29     {
    30         parent[x]=y;
    31         if(ranks[x]==ranks[y])
    32             ranks[y]++;
    33     }
    34 }
    35 struct node
    36 {
    37     int u,v,w;
    38     node(int uu,int vv,int ww){u=uu,v=vv,w=ww;}
    39     node(){u=0,v=0,w=inf;}
    40 }edge[maxn*maxn];
    41 bool cmp(node a,node b)
    42 {
    43     return a.w<b.w;
    44 }
    45 int kruskal()
    46 {
    47     sort(edge,edge+E,cmp);
    48     init(V);
    49     int ans=0;
    50     for(int i=0;i<E;i++)
    51     {
    52         node a=edge[i];
    53         if(finds(a.u)!=finds(a.v))
    54         {unite(a.u,a.v);
    55          ans+=a.w;
    56         }
    57     }
    58     return ans;
    59 }
    60 int main()
    61 {
    62     int k,w;
    63     char u,v;
    64     while(~scanf("%d",&V),V)
    65     {
    66         E=0;
    67         for(int i=0;i<maxn;i++)
    68             edge[i]=node();
    69         for(int i=0;i<V-1;i++)
    70         {
    71             cin>>u>>k;
    72             while(k--)
    73             {
    74                 cin>>v>>w;
    75                 edge[E++]=node(u-'A',v-'A',w);
    76             }
    77         }
    78         cout<<kruskal()<<endl;
    79     }
    80     return 0;
    81 }

    题目大意:

    给定相互联通的边和权值,求最小生成树。

    思路:

    因为是稀疏图,推荐用Kruskal。

    注意点:

    在这道题中学会了给结构体的对象设初值node(){u=0,v=0,w=inf;}和赋值node(int uu,int vv,int ww){u=uu,v=vv,w=ww;}。

  • 相关阅读:
    GO学习-(17) Go语言基础之反射
    Go语言基础之time包
    Go语言标准库log介绍
    GO学习-(16) Go语言基础之文件操作
    GO学习-(15) Go语言基础之包
    GO学习-(14) Go语言基础之接口
    五种开源API网关实现组件对比
    Spring Junit4 Test
    Java泛型
    SQL 基本(Head First)
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/10769839.html
Copyright © 2020-2023  润新知