您的位置:首页 > 其它

poj-3723 Conscription 最大生成树

2014-07-31 20:02 411 查看
题目链接

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 20005;
const int Mod = 1000000007;
int n,m,R,ans;
int p[maxn];
struct node
{
int x,y,w;
}edge[maxn*3];
bool cmp( node a,node b )
{
return a.w > b.w;
}
int find( int x )
{
return p[x] == x?x:p[x] = find( p[x] );
}
void merge( int i )
{
int x = find( edge[i].x );
int y = find( edge[i].y );
if( x != y )
{
p[x] = y;
ans += edge[i].w;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas;
scanf("%d",&cas);
while( cas -- )
{
ans = 0;
scanf("%d%d%d",&n,&m,&R);
for( int i = 0; i <= n+m; i ++ )	p[i] = i;
for( int i = 0; i < R; i ++ )
{
scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w);
edge[i].y += n;
}
sort( edge,edge+R,cmp );
for( int i = 0; i < R; i ++ )
merge( i );
printf("%d\n",(n+m)*10000-ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: