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 87 88 89 90 91 92
| #include <bits/stdc++.h>
using namespace std;
#define int long long #define endl '\n'
#define PII pair<int, int> #define fi first #define se second
const int N = 407, INF = 1e9 + 7, NPOS = -1; struct Matrix { int n; int a[N][N]; }; struct KuhnMunkres : Matrix { int hl[N], hr[N], slk[N]; int fl[N], fr[N], vl[N], vr[N], pre[N], q[N], ql, qr; int check(int i) { if (vl[i] = 1, fl[i] != NPOS) return vr[q[qr++] = fl[i]] = 1; while (i != NPOS) swap(i, fr[fl[i] = pre[i]]); return 0; } void bfs(int s) { fill(slk, slk + n, INF), fill(vl, vl + n, 0), fill(vr, vr + n, 0); for (vr[q[ql = 0] = s] = qr = 1;;) { for (int d; ql < qr;) for (int i = 0, j = q[ql++]; i < n; ++i) if (!vl[i] && slk[i] >= (d = hl[i] + hr[j] - a[i][j])) if (pre[i] = j, d) slk[i] = d; else if (!check(i)) return; int d = INF; for (int i = 0; i < n; ++i) if (!vl[i] && d > slk[i]) d = slk[i]; for (int i = 0; i < n; ++i) { if (vl[i]) hl[i] += d; else slk[i] -= d; if (vr[i]) hr[i] -= d; } for (int i = 0; i < n; ++i) if (!vl[i] && !slk[i] && !check(i)) return; } } void ask() { fill(fl, fl + n, NPOS), fill(fr, fr + n, NPOS), fill(hr, hr + n, 0); for (int i = 0; i < n; ++i) hl[i] = *max_element(a[i], a[i] + n); for (int j = 0; j < n; ++j) bfs(j); } } km; struct Istream { char b[20 << 20], *i, *e; Istream(FILE *in) : i(b), e(b + fread(b, sizeof(*b), sizeof(b) - 1, in)) {} Istream &operator>>(int &val) { while (*i < '0') ++i; for (val = 0; *i >= '0'; ++i) val = (val << 3) + (val << 1) + (*i - '0'); return *this; } } kin(stdin); #define cin kin
void solve() { int nl, nr, m, u, v; for (cin >> nl >> nr >> m; m--; cin >> km.a[u][v]) cin >> u >> v; km.n = max(nl, nr) + 1, km.ask(); long long ans = 0; for (int i = 1; i <= nl; ++i) ans += km.a[i][km.fl[i]]; cout << ans << '\n'; for (int i = 1; i <= nl; ++i) cout << (km.a[i][km.fl[i]] ? km.fl[i] : 0) << ' '; }
signed main() { solve(); }
|