题意:给定 n 个男人,m 个女人,和 r 个男女之间的关系,每个征募一个人要用10000元,但是如果有关系可以少花一些钱,即10000-亲密度,
求一个最小要花多少钱。
析:最后生成的关系肯定是一片森林,也就是最大权森林,但是我可以把权值取反,然后就是一个求最小森林了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 10000 + 10; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Node{ int u, v, val; Node() { } Node(int uu, int vv, int va) : u(uu), v(vv), val(va) { } bool operator < (const Node &p) const{ return val < p.val; } }; Node a[maxn*5]; int p[maxn*2]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int solve(int r){ sort(a, a + r); int ans = 0; for(int i = 0; i < r; ++i){ int x = Find(a[i].u); int y = Find(a[i].v); if(x != y) p[y] = x, ans += a[i].val; } return ans; } int main(){ int T; cin >> T; while(T--){ int r; 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){ int u, v, val; scanf("%d %d %d", &u, &v, &val); a[i] = Node(u, v+n, -val); } printf("%d\n", 10000 * (n+m) + solve(r)); } return 0; }