#include<bits/stdc++.h>
using namespace std;
int Father[5000];
int ans = 0;
struct Node {
int u, v, e, s;
bool operator < (const Node &p)const { return s == p.s ? e< p.e : s>p.s; }
}node[5000];
int FindFather(int x) { return Father[x] < 0 ? x : FindFather(Father[x]); }
void Umerge(Node x)
{
int Fx = FindFather(x.u);
int Fy = FindFather(x.v);
if (Fx != Fy)
{
Father[Fy] += Father[Fx];
Father[Fx] = Fy;
if (x.s == 0)ans += x.e;
}
}
int main() {
int n;
while (cin >> n)
{
if (n == 0)break;
memset(Father, -1, sizeof(Father));
int m = n * (n - 1) / 2;
for (int i = 0; i<m; ++i) cin >> node[i].u >> node[i].v >> node[i].e >> node[i].s;
sort(node, node + m);
for (int i = 0; i <m; i++) Umerge(node[i]);
cout << ans << endl;
}
}
HDU 1879 继续畅通工程(带S的最小生成树Kruskal)
猜你喜欢
转载自blog.csdn.net/qq_31741481/article/details/83239962
今日推荐
周排行