题意:有n个女兵m个男兵,如果他们之间认识,那么征兵的时候可以省钱,省得数目是10000-他和她的亲密度。
思路:有个思想是把权值取反,最终再加上原本的权值。
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int Nmax = 50005;
class Node
{
public:
int s,e,v;
}edge[Nmax];
int dist[Nmax];
int f[Nmax],num[Nmax];
int n,m,r;
int cmp(Node a,Node b)
{
return a.v < b.v;
}
int Find(int x)
{
if(x == f[x])
return x;
else return f[x] = Find(f[x]);
}
int Union(int x,int y)
{
x = Find(x);
y = Find(y);
if(x == y)
return 0;
else if(num[x] > num[y]){
f[y] = x;
num[x] += num[y];
}
else {
f[x] = y;
num[y] += num[x];
}
return 1;
}
int main()
{
//freopen("in.txt","r",stdin);
int ncase;
scanf("%d",&ncase);
while(ncase--){
scanf("%d%d%d",&n,&m,&r);
for(int i = 1;i <= n+m; i++){
f[i] = i;
num[i] = 0;
}
int a,b,c;
for(int i = 1;i <= r; i++){
scanf("%d%d%d",&a,&b,&c);
edge[i].s = a+1;
edge[i].e = b+n+1;
edge[i].v = -c;
}
int sum = 0;
sort(edge+1,edge+r+1,cmp);
for(int i = 1;i <= r; i++){
if(Union(edge[i].s,edge[i].e)){
sum += edge[i].v;
}
}
printf("%d\n",10000*(n+m)+sum);
}
return 0;
}