标签:style blog http color os java io for ar
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
3 5
Huge input, scanf is recommended.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 struct arc{ 18 int u,v,w; 19 arc(int x = 0,int y = 0,int z = 0){ 20 u = x; 21 v = y; 22 w = z; 23 } 24 }; 25 arc e[10000]; 26 int n,m,uf[110]; 27 bool cmp(const arc &x,const arc &y){ 28 return x.w < y.w; 29 } 30 int Find(int x){ 31 if(x != uf[x]) uf[x] = Find(uf[x]); 32 return uf[x]; 33 } 34 int Kruskal(){ 35 for(int i = 0; i <= n; i++) uf[i] = i; 36 sort(e,e+m,cmp); 37 int ans = 0; 38 for(int i = 0; i < m; i++){ 39 int x = Find(e[i].u); 40 int y = Find(e[i].v); 41 if(x != y){ 42 uf[x] = y; 43 ans += e[i].w; 44 } 45 } 46 return ans; 47 } 48 int main() { 49 int i,u,v,w; 50 while(scanf("%d",&n),n){ 51 m = n*(n-1)/2; 52 for(i = 0; i < m; i++){ 53 scanf("%d %d %d",&u,&v,&w); 54 e[i] = arc(u,v,w); 55 } 56 printf("%d\n",Kruskal()); 57 } 58 return 0; 59 }
标签:style blog http color os java io for ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3940971.html