题目 : Problem - 1879 (hdu.edu.cn)
#include<bits/stdc++.h>
using namespace std;
#define MXN 5000
int a[MXN],ans;
struct town{
int a,b,c,d;
}t[MXN];
bool cmp(town x,town y){
return x.c<y.c;
}
int findx(int x){
while(a[x]!=x){
x=a[x];
}
return x;
}
void merge(town&t){
int fx=findx(t.a);
int fy=findx(t.b);
if(fx!=fy){
a[fx]=fy;
ans+=t.c;
}
}
int main(){
int N,M;
while(scanf("%d",&N),N){
M=N*(N-1)/2;
for(int i=1;i<=N;i++){
a[i]=i;
}
for(int i=1;i<=M;i++){
scanf("%d %d %d %d",&t[i].a,&t[i].b,&t[i].c,&t[i].d);
if(t[i].d==1){
t[i].c=0;
}
}
sort(t+1,t+M+1,cmp);
ans=0;
for(int i=1;i<=M;i++){
merge(t[i]);
}
printf("%d\n",ans);
}
return 0;
}