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.
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
最小生成树果题,我分别用prime和Kruskal敲一遍代码<span style="font-family:Microsoft YaHei;">(G++超时,C++就AC....)</span>
<span style="font-family:Microsoft YaHei;">首先是prime,注意把已有道路的地方之间的距离都设为0</span> #include<stdio.h> #define inf 9999999 int map[505][505]; int num[505],vis[505],di[505]; int n; int pri() { int i,j,v,min,res=0; for(i=1;i<=n;i++) { di[i]=inf; vis[i]=0; } di[1]=0;vis[i]=1;v=1; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) if(!vis[j]&&map[v][j]<di[j]) di[j]=map[v][j]; min=inf; for(j=1;j<=n;j++) if(!vis[j]&&min>di[j]) { v=j; min=di[j]; } vis[v]=1; res+=min; } return res; } int main() { int m,k,t; int i,j; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); for(i=1;i<=n;i++) { map[i][i]=0; for(j=i+1;j<=n;j++) map[i][j]=map[j][i]=inf; } int u,v,w; while(m--) { scanf("%d%d%d",&u,&v,&w); if(w<map[u][v]) map[u][v]=map[v][u]=w; } while(k--) { scanf("%d",&m); for(i=0;i<m;i++) scanf("%d",&num[i]); for(i=0;i<m;i++) for(j=i+1;j<m;j++) map[num[i]][num[j]]=map[num[j]][num[i]]=0; } int ans=pri(); if(ans>=inf) printf("-1\n"); else printf("%d\n",ans); } return 0; }
这是kruskal,注意需要检查合并各课树之后是否只剩1棵树,不然就是无法连通各个城市
#include<stdio.h> #include<algorithm> using namespace std; struct edge { int u,v,w; }e[25000]; int n,p[505]; int cmp(edge a,edge b) { return a.w<b.w; } int find(int x) { return p[x]==x?x:find(p[x]); } int main() { int t; scanf("%d",&t); while(t--) { int i,j,m,k,x,y; scanf("%d%d%d",&n,&m,&k); for(i=1;i<=n;i++) p[i]=i; for(i=1;i<=m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); sort(e+1,e+m+1,cmp); int num,root,a; for(i=1;i<=k;i++) { scanf("%d%d",&num,&root); for(j=1;j<num;j++) { scanf("%d",&a); x=find(root); y=find(a); if(x!=y) p[x]=y; } } int ans=0; for(num=0,i=1;i<=n;i++) if(p[i]==i) num++; for(i=1;i<=m;i++) { x=find(e[i].u); y=find(e[i].v); if(x!=y) { p[x]=y; ans+=e[i].w; num--; } if(num==1) break; } if(num==1)//能够合并成一个树 printf("%d\n",ans); else//存在两个及以上的树,即不能全部城市连通 printf("-1\n"); } return 0; }