标签:
Description
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX = 5000; int p[MAX]; struct edge{ int s; int e; int w; int i; }a[MAX]; bool cmp(edge i,edge j){ return i.w <j.w; } int find(int x){ return x == p[x] ? x:p[x] = find(p[x]); } int main() { int T; while(~scanf("%d",&T)&&T){ int sum = 0; for(int i = 1; i <= T;i++) scanf("%d%d%d%d",&a[i].s,&a[i].e,&a[i].w,&a[i].i); for(int i = 1; i <= T;i++) p[i] = i; for(int i = 1; i <= T;i++) if(a[i].i == 1) a[i] .w = 0; sort(a+1,a+T+1,cmp); for(int i = 1; i <=T;i++){ int fx = find(a[i].s),fy = find(a[i].e); if(fx != fy){ p[fx] = fy; sum += a[i].w; } } printf("%d\n",sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4322129.html