【最小生成树】Connect the Cities

Connect the Cities

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


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
 题目大意:
  输入T,表示有T组数据,第一行输入N,M,K,表示有N个点,M条边,K个已经连通的集合。
  然后有M行,每行输入 A,B,C表示A-B的边权为C,
  然后有K行,每一行先输入k,表示有k个点是相互连通了的。
  问你连通这些所有点,需要的最小的边权总和是多少?
解法:Kruskal+并查集
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #define MAX 100005
 6 using namespace std;
 7 int ID[MAX];/*ID[i]=i表示i为独立点*/
 8 struct STD
 9 {
10     int x,y,z;
11     bool operator < (const STD &p) const /*结构体自定义函数*/
12     {                                        /*结构体自定义函数*/
13        return z < p.z;             /*按z的大小由小到大排序 */
14     }
15 }stu[MAX];
16 int SIGN_Num;
17 void Cread(int N)
18 {
19     SIGN_Num=N;
20     for(int i=0;i<=N;i++)ID[i]=i;
21 }
22 int Find(int x)/*寻找父亲节点,递归形式,有时需要栈扩展*/
23 {
24     int tmp;
25     if(ID[x]!=x)tmp=Find(ID[x]);
26     else return x;
27     ID[x]=tmp;  /*路径压缩*/
28     return tmp;
29 }
30 
31 int Add(int x,int y)/*添加点操作*/
32 {
33     x=Find(x);
34     y=Find(y);
35     if(x!=y)
36     {
37         ID[x]=y;
38         return 1;
39     }
40     else return 0;
41 }
42 int Kruskal(int M)/*如果可构成最小生成树,返回最小权值*/
43 {         /*否则返回-1,表示无法构成最小生成树,O(N^3)*/
44     int i,j,k,ii,jj;
45     int Sum_Min=0,MIN;
46     for(k=1;k<=M;k++)
47     {
48         ii=stu[k].x;
49         jj=stu[k].y;
50         MIN=stu[k].z;
51         if(Add(ii,jj))
52         {
53             Sum_Min+=MIN;
54             SIGN_Num--;
55         }
56     }
57     if(SIGN_Num==1)return Sum_Min;
58     return -1;
59 }
60 int main()
61 {
62     int T,i,j,k,N,M,K;
63     scanf("%d",&T);
64     while(T--)
65     {
66         scanf("%d%d%d",&N,&M,&K);
67         Cread(N);
68         int a,b,c;
69         for(i=1;i<=M;i++)
70         {
71             scanf("%d%d%d",&a,&b,&c);
72             stu[i].x=a;stu[i].y=b;stu[i].z=c;
73         }
74         sort(stu+1,stu+M+1);
75         for(i=0;i<K;i++)
76         {
77             int TMD;
78             scanf("%d",&TMD);
79             scanf("%d",&a);
80             while(--TMD)
81             {
82                 scanf("%d",&b);
83                 if(Add(b,a))SIGN_Num--;
84             }
85         }
86         int Min=Kruskal(M);
87         if(Min==-1)printf("-1\n");
88         else printf("%d\n",Min);
89     }
90     return 0;
91 }
View Code

 

posted @ 2015-06-09 01:48  Wurq  阅读(167)  评论(0编辑  收藏  举报