刚看完问题有点懵逼,二分图匹配?最大带权二分图匹配?
本来想学学KM算法,结果发现有O(N^3),告辞~~
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<queue> using namespace std; #define maxn 55000 #define INF 10100 struct Node { int be; int en; int len; }que[maxn]; bool bml(Node a, Node b) { return a.len < b.len; } int par[maxn]; int find(int x) { if (par[x] != -1) { return par[x] = find(par[x]); } return x; } int main() { long long ans = 0; int T, n, m, r; int be, en, len; scanf("%d", &T); while (T--) { scanf("%d %d %d", &n, &m, &r); memset(par, -1, sizeof(par)); for (int i = 0; i < r; i++) { scanf("%d %d %d", &que[i].be, &que[i].en, &que[i].len); que[i].en += INF; que[i].len = -que[i].len; } sort(que, que + r, bml); for (int i = 0; i < r; i++) { int a = find(que[i].be); int b = find(que[i].en); if (a != b) { ans += que[i].len; par[a] = b; } } printf("%lld\n", 10000*(n+m) + ans); ans = 0; } return 0; }