kruskal算法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=105;
int f[MAX],t;
struct edge_ {
int s,e,v;
bool ok;
} edge[MAX];
bool cmp(edge_ a,edge_ b) {
return a.v<b.v;
}
int find(int a) {
if(a!=f[a])
a=find(f[a]);
return a;
}
int kruskal() {
int sum=0;
for(int i=0; i<MAX; i++)
f[i]=i;
sort(edge,edge+t,cmp);
for(int i=0; i<t; i++)
if(edge[i].ok) {
int f1=find(edge[i].s),f2=find(edge[i].e);
f[f1]=f2;
}
for(int i=0; i<t; i++)
if(!edge[i].ok) {
int f1=find(edge[i].s),f2=find(edge[i].e);
if(f1!=f2) {
sum+=edge[i].v;
f[f1]=f2;
}
}
return sum;
}
int main() {
int n;
while(scanf("%d",&n)!=EOF) {
t=(n-1)*n/2;
for(int i=0; i<t; i++)
scanf("%d%d%d%d",&edge[i].s,&edge[i].e,&edge[i].v,&edge[i].ok);
printf("%d\n",kruskal());
}
}