Connect the Cities

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

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

思路:最小生成树(kruskal)

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <string>
 7 #include <cstdlib>
 8 using namespace std;
 9 
10 const int maxn=510,maxm=25010;
11 int f[maxn];
12 int n,m,cnt,ans,conv,fn,fto,d,t,k;
13 
14 struct qq
15 {
16     int n,to,d;
17     friend bool operator < (qq a,qq b)
18     {
19         return a.d<b.d;
20     }
21 } e[maxm];
22 
23 void close()
24 {
25 exit(0);
26 }
27 
28 int getfather(int k)
29 {
30     if (f[k]==k)
31         return k;
32     f[k]=getfather(f[k]);
33     return f[k];
34 }
35 void work()
36 {
37     ans=0;
38     int cnt=0;
39     for (int i=1;i<=n;i++)
40         if (f[i]==i)
41             cnt++;
42     for (int i=1;i<=m;i++)
43     {
44         fn=getfather(e[i].n);
45         fto=getfather(e[i].to);
46         if (fn==fto)
47             continue;
48         else
49         {
50             cnt--;
51             ans+=e[i].d;
52             f[fn]=fto;
53         }
54     }
55     if (cnt==1)
56         printf("%d\n",ans);
57     else
58         printf("-1\n");
59 }
60 
61 void init()
62 {
63     while (scanf("%d",&t)!=EOF)
64     {
65         while (t--)
66         {
67             scanf("%d %d %d",&n,&m,&k);
68             for (int i=1;i<=n;i++)
69                 f[i]=i;
70             for (int i=1;i<=m;i++)
71                 scanf("%d %d %d",&e[i].n,&e[i].to,&e[i].d);
72             for (int i=1;i<=k;i++)
73             {
74                 int x=0,p,y;
75                 f[0]=0;
76                 scanf("%d",&p);
77                 for (int j=1;j<=p;j++)
78                 {
79                     scanf("%d",&y);
80                     int fy=getfather(y);
81                     int fx=getfather(x);
82                     if (fy!=fx)
83                         f[fx]=fy;
84                     x=y;
85                 }
86             }
87             sort(e+1,e+m+1);
88             work();
89         }
90     }
91 }
92 
93 
94 int main ()
95 {
96     init();
97     close();
98     return 0;
99 }

 

posted on 2013-04-26 22:33  cssystem  阅读(138)  评论(0编辑  收藏  举报