题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233
题意:中文题
解析:最小生成树入门题
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
struct node
{
int x,y,v;
bool operator < (const node &b)const
{
return v<b.v;
}
}a[maxn];
int fa[maxn];
void init(int n)
{
for(int i=0;i<=n;i++)
fa[i] = i;
}
int getfa(int x)
{
if(x==fa[x])
return x;
return fa[x] = getfa(fa[x]);
}
int main(void)
{
int n,m;
while(~scanf("%d",&n)&&n)
{
m = n*(n-1)/2;
init(n);
for(int i=0;i<m;i++)
scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].v);
sort(a,a+m);
int ans = 0;
for(int i=0;i<m;i++)
{
int t1 = getfa(a[i].x);
int t2 = getfa(a[i].y);
if(t1!=t2)
{
fa[t1] = t2;
ans += a[i].v;
}
}
printf("%d\n",ans);
}
return 0;
}