HDU1233

裸mst

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 struct node{
 5     int from,to,w;
 6     bool operator < (const node &a){
 7         return w<a.w;
 8     }
 9 }a[10010];
10 int n,m,f[101],ans;
11 
12 void ini(){
13     ans=0;
14     for (int i=1;i<=n;i++) f[i]=i;
15 }
16 
17 int getf(int u){
18     return u==f[u]?f[u]:f[u]=getf(f[u]);
19 }
20 
21 void merge(int u,int v){
22     f[getf(u)]=getf(v);
23 }
24 
25 void solve()
26 {
27     while (cin>>n){
28         if (n==0) break;
29         ini();
30         m=n*(n-1)/2;
31         for (int i=1;i<=m;i++) cin>>a[i].from>>a[i].to>>a[i].w;
32         sort(a+1,a+1+m);
33         for (int i=1;i<=m;i++){
34             if (getf(a[i].from)!=getf(a[i].to)){
35                 ans+=a[i].w;
36                 merge(a[i].from,a[i].to);
37             }
38         }
39         cout<<ans<<endl;
40     }
41 }
42 
43 int main()
44 {
45     solve();
46     return 0;
47 }

猜你喜欢

转载自www.cnblogs.com/whiteli/p/12812742.html
今日推荐