wa了两回才A!不能忍,本来和1875差不多的。。。push_back忘-1了。。。。
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 5100;
struct P
{
int from, to, value;
P(int a, int b, int c):from(a), to(b), value(c){};
};
bool cmp(P a, P b)
{
return a.value < b.value;
}
int father[N], son[N];
int sum;
void init(int n)
{
sum = 0;
for(int i = 0; i < n; i++)
{
father[i] = i;
son[i] = 1;
}
}
int findf(int x)
{
return (x == father[x]) ? x : (father[x] = findf(father[x]));
}
bool unoin(int p, int q)
{
int fp = findf(p);
int fq = findf(q);
if(fp == fq)return 0;
if(son[fp] > son[fq])
{
father[fq] = fp;
son[fp] += son[fq];
}
else
{
father[fp] = fq;
son[fq] += son[fp];
}
return 1;
}
int main()
{
int n;
int T;
while(scanf("%d", &n),n)
{
vector<P> p;
init(n);
int len = n * (n - 1) / 2;
for(int i = 0; i < len; i++)
{
int st, en, va, f;
scanf("%d%d%d%d", &st, &en, &va, &f);
if(!f)
{
p.push_back(P(st - 1, en - 1, va));
}
else
{
unoin(st - 1,en - 1);
}
}
sort(p.begin(), p.end(),cmp);
for(int i = 0; i < p.size(); i++)
{
if(unoin(p[i].from, p[i].to))
{
sum += p[i].value;
}
}
printf("%d\n", sum);
}
return 0;
}