Article Directory
Subject to the effect
Problem-solving ideas
The people who have been recruited seen as minimum spanning tree node. Simulation of the minimum spanning tree construction process. (But here it is the maximum spanning tree)
Code
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXR = 50000+5;
struct Edge
{
int s;
int t;
int w;
bool operator < (const Edge &A)const
{
return w > A.w;
}
}G[MAXR];
int father[MAXR];
void init(int N)
{
for(int i=0; i<N; i++)
father[i] = -1;
}
int find_father(int v)
{
if(father[v] == -1)
return v;
int tmp = find_father(father[v]);
father[v] = tmp;
return tmp;
}
int main()
{
int T;
int a, b, w;
int cnt;
scanf("%d", &T);
while(T--)
{
cnt = 0;
int N, M, R;
scanf("%d%d%d", &N, &M, &R);
init(N+M);
while(R--)
{
scanf("%d%d%d", &a, &b, &w);
b += N; //注意给的是分别在男生和女生中的标签
G[cnt].s = a;
G[cnt].t = b;
G[cnt++].w = w;
}
sort(G, G+cnt);
ll ans = 0;
for(int i=0; i<cnt; i++)
{
Edge e = G[i];
int fa = find_father(e.s);
int fb = find_father(e.t);
if(fa != fb)
{
ans += e.w;
father[fa] = fb;
}
}
printf("%lld\n", 10000*(N+M) - ans);
}
return 0;
}