标签:ons names ring while pre clu cstring main mes
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=110; int p[N],n,m,ans,num; struct node { int a,b,w; }e[10005]; bool cmp(node a, node b) { return a.w < b.w; } int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } void kru() { for(int i=0;i<m;++i) { int a =e[i].a,b=e[i].b,c=e[i].w; a=find(a); b=find(b); if(a!=b) { num++; ans+=c; p[a] = b; } } } int main() { while(cin>>n&&n) { ans=0; num=0; m =n*(n-1)/2; for(int i=1;i<=n;++i) p[i]=i; for(int i=0;i<m;++i) cin>>e[i].a>>e[i].b>>e[i].w; sort(e,e+m,cmp); kru(); cout<<ans<<endl; } return 0; }
标签:ons names ring while pre clu cstring main mes
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12240427.html