标签:
Description
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX = 5000; struct edge{ int s; int e; int w; }a[MAX]; bool cmp(edge a,edge b){ return a.w < b.w; } int p[MAX]; int find(int x){ return x == p[x]?x:p[x] = find(p[x]); } int main() { int N,M; while(~scanf("%d",&N)&&N){ M = N*(N-1)/2; memset(p,0,sizeof(p)); memset(a,0,sizeof(a)); for(int i = 1; i <= N;i++) p[i] = i; for(int i = 1 ; i<= M;i++) scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].w); sort(a+1,a+M+1,cmp); int sum = 0; for(int i = 1;i <= M ;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/4322097.html