标签:pos ota hdoj name disco ++i php ble output
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
1
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct graph{ int a; int b; int cost; }G[ 25005]; int root[505]; int n,m; int find(int i){ if(root[i]==i) return i; return root[i]=find(root[i]); } void unio(int i,int j){ int t=find(i); int k=find(j); if(t<=k) root[k]=t; else root[t]=k; return ; } int cmp(graph u,graph v){ return u.cost<v.cost; } int shortest; void kruskal(){ sort(G,G+m,cmp); int i,t,k; shortest=0; for(i=0;i<m;++i){ t=find(G[i].a); k=find(G[i].b); if(t!=k){ unio(t,k); shortest+=G[i].cost; } } return ; } int main(){ int T; scanf("%d",&T); while(T--){ int k,i,j,p,q,c,t,x,y; scanf("%d%d%d",&n,&m,&k); for(i=1;i<=n;++i) root[i] = i; for(i=0;i<m;++i){ scanf("%d%d%d",&p,&q,&c); G[i].a=p,G[i].b=q,G[i].cost=c; } for(i=1;i<=k;++i){ scanf("%d",&t); scanf("%d%d",&x,&y); unio(x,y); for(j=3;j<=t;++j){ scanf("%d",&y); unio(x,y); } } kruskal(); int nu=0; for(i=1;i<=n;++i){ if(find(i)==i) nu++; } if(nu>1) printf("-1\n"); else printf("%d\n",shortest); } return 0; }
hdoj-3371-Connect the Cities【最小生成树】
标签:pos ota hdoj name disco ++i php ble output
原文地址:http://www.cnblogs.com/yxysuanfa/p/6871574.html