并查集都写错!!!
#include <iostream>
#include <cstring>
const int N = 10010;
using namespace std;
struct node
{
int u, v, w;
bool operator < (const node &cmp) const
{
return (w<cmp.w);
}
}edge[N];
int f[N];
int find(int x)
{
if (f[x]!=x) f[x] = find(f[x]);
return f[x];
}
int main()
{
int n, m;
int u, v, w;
while (~scanf("%d", &n) && n)
{
m = (n*(n-1)) / 2;
for (int i=0;i<m;++i)
{
scanf("%d %d %d", &u, &v, &w);
if (u>v) swap(u,v);
edge[i].u = u;
edge[i].v = v;
edge[i].w = w;
}
sort(edge, edge+m);
for (int i=1;i<=n;++i) f[i] = i;
w = 0; v = 1; u = 0;
while (v<n)
{
int x = find(edge[u].u);
int y = find(edge[u].v);
if (x!=y)
{
++v;
w += edge[u].w;
if (x>y) swap(x, y);
f[y] = x;
}
++u;
}
printf("%d\n", w);
}
return 0;
}