浙大2008年研究生机试
// 畅通工程
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int fa[110];
struct Node{
int s;
int e;
int c;//cost
int f;//flag
bool operator < (const Node & A) const {
return f > A.f;
}// 已修通的路排在前面
};
bool cmp(Node x, Node y){
return x.c < y.c;
}// 对未修的路排序,成本低在前
int Getfa(int x){
if(x == fa[x])
return x;
fa[x] = Getfa(fa[x]);
return fa[x];
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("E:\\in.txt", "r", stdin);
#endif
int n;
while(scanf("%d", &n) && n){
int i, nr = n*(n-1)/2 ;
for(i=1; i<=n; i++){
fa[i]=i;
}
Node buf[10000];
for(i=0; i<nr; i++){
scanf("%d %d %d %d", &buf[i].s, &buf[i].e, &buf[i].c, &buf[i].f);
}
sort(buf, buf+nr);
for(i=0; buf[i].f == 1 && i<nr; i++){
if(Getfa(buf[i].s) != Getfa(buf[i].e)){
fa[Getfa(buf[i].s)] = Getfa(buf[i].e);
}
}// 连通已修好的路
sort(buf+i, buf+nr, cmp);//对维修的路排序
int ans=0;
for(; i<nr; i++){
if(Getfa(buf[i].s) != Getfa(buf[i].e)){
fa[Getfa(buf[i].s)] = Getfa(buf[i].e);
ans += buf[i].c;
}
}
printf("%d\n", ans);
}//while
return 0;
}