思路:水!
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 10010 using namespace std; int fa[101]; int n,m,ans,tot; struct nond{ int x,y,z; }v[MAXN]; int cmp(nond a,nond b){ return a.z<b.z; } int find(int x){ if(fa[x]==x) return x; else return fa[x]=find(fa[x]); } int main(){ while(scanf("%d",&n)&&n){ m=n*(n-1)/2; for(int i=1;i<=m;i++) scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z); sort(v+1,v+1+m,cmp); for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=m;i++){ int dx=find(v[i].x); int dy=find(v[i].y); if(dx==dy) continue; fa[dy]=dx;tot++; ans+=v[i].z; if(tot==n-1) break; } printf("%d\n",ans);ans=0;tot=0; } }