标签:
JGShining
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int MAX = 150; 6 int f[MAX]; 7 struct road{ 8 int c1; int c2; int l; 9 }r[MAX*MAX]; 10 11 bool cmp(const road &r1, const road &r2) 12 { 13 return r1.l<r2.l; 14 } 15 int sf(int x) 16 { 17 return x == f[x] ? x : f[x] = sf(f[x]); 18 } 19 int main() 20 { 21 int n, m, ans,cnt; 22 while (scanf("%d", &n) != EOF&&n) 23 { 24 ans = 0; cnt = 0; 25 m = n*(n - 1) / 2; 26 27 for (int i = 1; i <= n; i++) 28 f[i] = i; 29 for (int i = 1; i <= m; i++) 30 scanf("%d%d%d", &r[i].c1, &r[i].c2, &r[i].l); 31 sort(r + 1, r + m + 1, cmp); 32 33 int i = -1; 34 while (cnt < n - 1) 35 { 36 int q = sf(r[++i].c1); 37 int w = sf(r[i].c2); 38 if (q != w) 39 { 40 cnt++; f[q] = w; ans += r[i].l; 41 } 42 } 43 printf("%d\n", ans); 44 } 45 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5169933.html