有一些地方需要铺盖电缆,这些地方两点间的路可能不止一条,需要求出来至少需要多少电缆才能让所有的点都连接起来,当然,间接连接也算。
/////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define maxn 105
struct node
{
int u, v, len;
friend bool operator < (node a, node b)
{
return a.len > b.len;
}
};
int f[maxn];
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
int main()
{
int N, M;
while(scanf("%d", &N) != EOF && N)
{
int i, ans=0;
priority_queue<node>Q;
node s;
for(i=0; i<=N; i++)
f[i] = i;
scanf("%d", &M);
while(M--)
{
scanf("%d%d%d", &s.u, &s.v, &s.len);
Q.push(s);
}
while(Q.size())
{
s = Q.top();Q.pop();
int u = Find(s.u), v = Find(s.v);
if(u != v)
{
f[u] = v;
ans += s.len;
}
}
printf("%d ", ans);
}
return 0;
}
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define maxn 105
struct node
{
int u, v, len;
friend bool operator < (node a, node b)
{
return a.len > b.len;
}
};
int f[maxn];
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
int main()
{
int N, M;
while(scanf("%d", &N) != EOF && N)
{
int i, ans=0;
priority_queue<node>Q;
node s;
for(i=0; i<=N; i++)
f[i] = i;
scanf("%d", &M);
while(M--)
{
scanf("%d%d%d", &s.u, &s.v, &s.len);
Q.push(s);
}
while(Q.size())
{
s = Q.top();Q.pop();
int u = Find(s.u), v = Find(s.v);
if(u != v)
{
f[u] = v;
ans += s.len;
}
}
printf("%d ", ans);
}
return 0;
}