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.
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
坑题!卡时间!
我看了好多网上的代码有的要求G++,有的要求C++,然而我用G++和C++交都超时...
不过经过优化后的无论用什么交都可以。
优化方式看代码。
我用kruskal写的代码风格有点乱希望不影响阅读(全在主函数里实现了....)
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int p[550]; 5 int u[255005]; 6 int v[255005]; 7 int w[255005]; 8 int r[255005]; 9 int n,m,k; 10 int finds(int i) 11 { 12 return p[i]==i?i:finds(p[i]); 13 } 14 int cmp(int a,int b) 15 { 16 return w[a]<w[b]; 17 } 18 int main() 19 { 20 int t; 21 scanf("%d",&t); 22 while(t--) 23 { 24 scanf("%d%d%d",&n,&m,&k); 25 for(int i=0;i<m;i++){ 26 scanf("%d%d%d",&u[i],&v[i],&w[i]); 27 } 28 for(int i=0;i<=m;i++){ 29 r[i]=i; 30 } 31 int temp; 32 int from,to; 33 for(int i=0;i<=n;i++) 34 p[i]=i; 35 for(int i=0;i<k;i++){ 36 scanf("%d%d",&temp,&from); 37 for(int i=1;i<temp;i++){ 38 scanf("%d",&to); 39 int x=finds(from); 40 int y=finds(to); 41 if(x!=y){ 42 p[y]=x; 43 } 44 } 45 } 46 int cnt=0; 47 for(int i=1;i<=n;i++){ 48 if(p[i]!=i) 49 cnt++; 50 } 51 int sum=0; 52 sort(r,r+m,cmp); 53 for(int i=0;i<m&&cnt<n-1;i++){///优化在此,如果统计边的过程中如果超过了n-1那么就不是树了,证明见离散课本。 54 int e=r[i]; 55 int x=finds(u[e]); 56 int y=finds(v[e]); 57 if(x!=y){ 58 p[y]=x; 59 cnt++; 60 sum+=w[e]; 61 //printf("w[%d]=%d ",e,w[e]); 62 } 63 } 64 if(cnt==n-1){ 65 printf("%d ",sum); 66 } 67 else 68 printf("-1 "); 69 } 70 }