#include<bits/stdc++.h>
using namespace std;
struct Node
{
int u, v, e;
bool operator <(const Node &p)const
{
return e < p.e;
}
}node[1000];
int Father[1000];
int FindFather(int x) { return Father[x] < 0 ? x : FindFather(Father[x]); }
int ans = 0;
void Umerge(Node x)
{
int Fx = FindFather(x.u);
int Fy = FindFather(x.v);
if (Fx != Fy)
{
Father[Fy] += Father[Fx];
Father[Fx] = Fy;
ans += x.e;
}
}
int main()
{
int n;
while (cin >> n&&n)
{
memset(Father, -1, sizeof(Father));
ans = 0;
int m = n * (n - 1) / 2;
for (int i = 0; i < m; i++)
{
cin >> node[i].u >> node[i].v >> node[i].e;
}
sort(node, node + m);
for (int i = 0; i < m; i++)
{
Umerge(node[i]);
}
cout << ans<<" ";
}
}
还是畅通工程 基础最小生成树★
最新推荐文章于 2022-05-29 20:37:33 发布