题目;http://acm.hdu.edu.cn/showproblem.php?pid=1233
#include <bits/stdc++.h>
using namespace std;
int parent[101];
struct edge
{
int a,b,dis;
}nums[5000];
int find(int x)//找根结点
{
if (x!= parent[x]) parent[x] = find(parent[x]);
return parent[x];
}
bool merge(int x,int y)//统一根结点
{
int a=find(x);
int b=find(y);
if (a!= b)
{
parent[a] = b;
}
}
bool cmp(edge u,edge v)
{
return u.dis<v.dis;//将距离从小到大排序
}
int main()
{
int n,m;
while (scanf("%d",&n)==1)
{
if(n==0) break;
for (int i = 1;i <= n;i++)
parent[i] = i;//将每个节点的根结点设为自身
m = n * (n - 1) / 2;
for (int i = 1;i <= m;i++)
scanf("%d %d %d",&nums[i].a,&nums[i].b,&nums[i].dis);
sort(nums+1,nums+1+m,cmp);
int s=0;
for (int i = 1;i <= m;i++)
{
if (find(nums[i].a) != find(nums[i].b))//如果俩村庄不属于统一根结点
{
merge(nums[i].a,nums[i].b);//统一根结点
s+= nums[i].dis;
}
}
printf("%d\n",s);
}
return 0;
}