1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include <iostream> #include <algorithm>
#define endl '\n'
using namespace std;
struct Edge { int u, v, w; bool operator < (const Edge &A) const { return w < A.w; } };
const int N = 2e4 + 5; const int M = 9e4 + 5; const int ORGL = 1e4;
int t, x, y, r; int n, m, cnt, ans; int fa[N]; struct Edge e[M];
inline void init() { ans = cnt = m = 0; for (int i = 1; i <= n; ++i) { fa[i] = i; } }
inline int find(const int &l) { return fa[l] == l ? l : fa[l] = find(fa[l]); }
inline int gid(const int &l, const bool &girl) { if (girl) { return l + 1; } else { return l + x + 1; } }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> t; while (t--) { cin >> x >> y >> r; n = x + y; init(); for (int i = 1; i <= r; ++i) { int xi, yi, vi; cin >> xi >> yi >> vi; int xid = gid(xi, true); int yid = gid(yi, false); e[++m].u = xid; e[m].v = yid; e[m].w = ORGL - vi; } for (int i = 1; i <= n; ++i) { e[++m].u = 0; e[m].v = i; e[m].w = ORGL; } sort(e + 1, e + m + 1); for (int i = 1; i <= m; ++i) { int fu = find(e[i].u); int fv = find(e[i].v); if (fu != fv) { fa[fu] = fv; ans += e[i].w; if (++cnt == n - 1) break; } } cout << ans + ORGL << endl; } }
|