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

 题意:有N个城市,M行代表两个城市之间连通的需要的价值,接下来K行,每行一个t,后面t个数表示后面的t个城市已经连通,求最小价值使所有城市连通,不能连通输出-1

 思路:最小生成树问题,需要注意的是输入量较大,用scanf输入

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<math.h>
 6 #include<algorithm>
 7 #define LL long long
 8 #define mod 1e9 + 7
 9 const int M = 505;
10 
11 using namespace std;
12 
13 struct node{
14     int x;
15     int y;
16     int value;
17 }a[M * 100];
18 
19 int cun[M];
20 int cnt;
21 
22 int cmp(node a, node b)
23 {
24     return a.value < b.value;
25 }
26 
27 int find(int x)
28 {
29     return cun[x] == x ? x : cun[x] = find(cun[x]);
30 }
31 
32 void shu(int x, int y)
33 {
34     int p = find(x);
35     int q = find(y);
36     if(p != q)
37     {
38         cnt++;
39         cun[p] = q;
40     }
41 }
42 
43 int main()
44 {
45     int n, m, k;
46     int t;
47     int p, flag, temp;
48     cin >> t;
49     while( t-- )
50     {
51         scanf("%d%d%d",&n,&m,&k);
52         for(int i = 1; i <= m; ++i)
53             scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].value);
54         for(int i = 0; i <= n; ++i)
55             cun[i] = i;
56         cnt = 1;
57         while( k-- )
58         {
59             scanf("%d%d",&p,&flag);
60             p--;
61             while( p-- )
62             {
63                 scanf("%d",&temp);
64                 shu(flag,temp);
65                 flag = temp;
66             }
67         }
68         sort(a + 1,a + m + 1,cmp);
69         int ans = 0;
70         for(int i = 1; i <= m; ++i)
71         {
72             if(find(a[i].x) != find(a[i].y))
73             {
74                 shu(a[i].x,a[i].y);
75                 ans += a[i].value;
76             }
77         }
78         if(cnt != n)
79             puts("-1");
80         else
81             cout << ans << endl;
82     }
83     return 0;
84 }
View Code

 

posted on 2014-08-06 21:32  Unico  阅读(117)  评论(0)    收藏  举报