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
| #include <stdio.h> #include <string.h> #include <iostream> using namespace std;
typedef long long LL; const int N = 440; const LL INF = 0x3f3f3f3f3f3f3f3f;
bool vis[N]; int pre[N], link[N], res[N]; int n, m, e; LL g[N][N], lx[N], ly[N], d[N], ans = 0;
void bfs(int k) { int x, y = 0; LL min1 = INF, delta; memset(vis, false, sizeof vis); memset(pre, 0, sizeof pre); memset(d, 0x3f, sizeof d); link[y] = k; do { x = link[y], delta = INF, vis[y] = true; for(int i=1; i<=m; ++i) { if(!vis[i]) { if(d[i] > lx[x] + ly[i] - g[x][i]) { d[i] = lx[x] + ly[i] - g[x][i]; pre[i] = y; } if(delta > d[i]) delta = d[i], min1 = i; } } for(int i=0; i<=m; ++i) if(vis[i]) lx[link[i]] -= delta, ly[i] += delta; else d[i] -= delta; y = min1; } while(link[y]); while(y) link[y] = link[pre[y]], y = pre[y]; }
int main() { scanf("%d%d%d", &n, &m, &e); m = max(n, m); for(int i=1; i<=e; ++i) { int u, v; LL w; scanf("%d%d%lld", &u, &v, &w); g[u][v] = w; lx[u] = max(lx[u], w); } for(int i=1; i<=n; ++i) bfs(i); for(int i=1; i<=m; ++i) { if(!g[link[i]][i]) continue; ans += g[link[i]][i]; res[link[i]] = i; } printf("%lld\n", ans); for(int i=1; i<=n; ++i) printf("%d ", res[i]); return 0; }
|