题目链接:http://poj.org/problem?id=3723#top
分析:一道模板题
AC代码
#include <cstdio>
#include <algorithm>
#include <iostream>using namespace std;struct edge
{int u,v,cost;
};
edge es[50050];
int x,y,z;
int par[20020];bool comp(const edge &n,const edge &m)
{return n.cost<m.cost;
}void init(int n)
{for(int i=0;i<n;i++){par[i]=i;}
}int findrt(int n)
{if(par[n]==n) return n;else return par[n]=findrt(par[n]);
}bool same(int n,int m)
{return findrt(n)==findrt(m);
}void unit(int n,int m) //注意
{int fn,fm;fn=findrt(n);fm=findrt(m);if(fm==fn) return ;par[fn]=fm;
}long long kur()
{sort(es,es+z,comp);init(x+y);long long ans=0;for(int i=0;i<z;i++){edge e=es[i];if(!same(e.u,e.v)){unit(e.u,e.v);ans+=e.cost;}}return ans;
}void solve()
{long long res=0;res=(x+y)*10000+kur();printf("%lld\n",res);
}int main()
{int t;cin>>t;while(t--){getchar();scanf("%d%d%d",&x,&y,&z);int a,b,c;for(int i=0;i<z;i++){scanf("%d%d%d",&a,&b,&c);es[i].u=a;es[i].v=b+x;es[i].cost=-c;}solve();}return 0;
}