标签:des style blog class code java
这个题数据真的有点大,我的程序跑了953ms,差一点超时啊
#include<iostream> #include<cstdio> #include<vector> #include<algorithm> using namespace std; const int MAX=1000000; struct node { int left,right,cost; }road[MAX]; bool cmp(node x,node y){return x.cost<y.cost;} int p[MAX],m,n; int find(int x){return x==p[x]? x:p[x]=find(p[x]);} int kruskal() { int ans=0,k=0; for (int i=0;i<=n;i++) p[i]=i; for (int i=0;i<m;i++) { int x=find(road[i].left); int y=find(road[i].right); if(x!=y) { ans+=road[i].cost; p[x]=y; k++; } if (k==n-1) break; } if (k==n-1)return ans; else return -1; } int main() { int x,y,c,k,num,a[500],T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); for (int i=0;i<m;i++) scanf("%d%d%d",&road[i].left,&road[i].right,&road[i].cost); while(k--) { scanf("%d",&num); for (int i=0;i<num;i++) { scanf("%d",&a[i]); for (int j=0;j<i;j++) { road[m].left=a[i]; road[m].right=a[j]; road[m].cost=0; m++; } } } sort(road,road+m,cmp); int mintree=kruskal(); printf("%d\n",mintree); } return 0; }
hdu 3371 Connect the Cities,布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/chensunrise/p/3719856.html