HDU 1879(最小生成树)
#include "iostream" #include "algorithm" #include "cstdio" using namespace std; const int maxn=200; typedef struct { int a,b,v,c; }node; int ans; node graph[maxn*(maxn-1)/2]; int father[maxn]; int Find(int x) { if(father[x]==x) return x; else { father[x]=Find(father[x]); return father[x]; } } void Union(int a,int b,int v,int c) { if(Find(a)!=Find(b)) { ans+=v; father[Find(a)]=Find(b); } } bool cmp(node a,node b) { return a.v<b.v; } int main() { int N,M; while(~scanf("%d",&N)){ if(N==0) break; M=N*(N-1)/2; ans=0; for(int i=0;i<M;i++) { scanf("%d%d%d%d",&graph[i].a,&graph[i].b,&graph[i].v,&graph[i].c); if(graph[i].c==1) graph[i].v=0; } for(int i=0;i<maxn;i++) father[i]=i; sort(graph,graph+M,cmp); for(int i=0;i<M;i++) Union(graph[i].a,graph[i].b,graph[i].v,graph[i].c); printf("%d\n",ans); } return 0; }