hdu3371Connect the Cities---最小生成树kruskal

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8568    Accepted Submission(s): 2404


Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  
 

 

Input
The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
 

 

Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
 

 

Sample Input
1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
 

 

Sample Output
1
 

 

Author
dandelion
 
题意:
告诉你一些城市,这些城市中有一些已经连接起来了,问最少的花费连接所有的城市
 
分析:
用kruskal最小生成树来做,
需要注意的有这么几点
先离线存储边的信息,
已经连接城市union在一起
并且还有重边需要注意
 
代码:
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <algorithm>
  5 using namespace std;
  6 
  7 const int maxn = 505;
  8 int map[maxn][maxn];
  9 
 10 struct Node
 11 {
 12     int a;
 13     int b;
 14     int value;
 15 }node[25005];
 16 
 17 bool cmp(Node n1, Node n2)
 18 {
 19     return n1.value < n2.value;
 20 }
 21 
 22 int fa[maxn];
 23 
 24 void init(int n)
 25 {
 26     for(int i = 0; i <= n; i++)
 27     fa[i] = i;
 28 }
 29 
 30 int find(int x)
 31 {
 32     return fa[x] == x ? x : fa[x]=find(fa[x]);
 33 }
 34 
 35 int sum;
 36 
 37 void unin(int u, int v, int value)
 38 {
 39     int fu = find(u);
 40     int fv = find(v);
 41     if(fu != fv)
 42     {
 43         sum += value;
 44         fa[fu] = fv;
 45     }
 46 }
 47 
 48 int main()
 49 {
 50     int t;
 51     int n, m, k;
 52     int p, q, c;
 53     int x, y, num;
 54     scanf("%d",&t);
 55     while(t--)
 56     {
 57         scanf("%d %d %d",&n, &m, &k);
 58         init(n);
 59         memset(map, -1, sizeof(map));//题目告诉两个城市之间的距离可能为零
 60         for(int i = 0; i < m; i++)
 61         {
 62             scanf("%d %d %d",&p, &q, &c);
 63             if(map[p][q] == -1)
 64                 map[p][q] = map[q][p] = c;
 65             else if(c < map[p][q])
 66                 map[p][q] = map[q][p] = c;
 67         }
 68         int l = 0;
 69         for(int i = 1; i < n; i++)
 70         {
 71             for(int j = i+1; j <= n; j++)
 72             {
 73                 if(map[i][j] != -1)
 74                 {
 75                     node[l].a = i;
 76                     node[l].b = j;
 77                     node[l++].value = map[i][j];
 78                 }
 79             }
 80         }
 81         sort(node, node+l, cmp);
 82         sum = 0;
 83         for(int i = 0; i < k; i++)
 84         {
 85             scanf("%d %d",&num, &x);
 86             num--;
 87             while(num--)
 88             {
 89                 scanf("%d",&y);
 90                 unin(x, y, 0);
 91             }
 92         }
 93         for(int i = 0; i < l; i++)//把l写成k   wrong的泪啊。。。。
 94             unin(node[i].a,node[i].b,node[i].value);
 95         bool flag = true;
 96         int find1 = find(1);
 97         for(int i = 2; i <= n; i++)
 98         {
 99             if(find(i) != find1)
100             {
101                 flag = false;
102                 break;
103             }
104         }
105         if(flag)
106         printf("%d\n",sum);
107         else
108         printf("-1\n");
109     }
110     return 0;
111 }
View Code

 

posted @ 2014-04-23 18:52  悠悠我心。  阅读(242)  评论(0编辑  收藏  举报