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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #define MAXN (100000 + 5) #define MAXM (500000 + 5) #define INF (0x7fffffff) #define MAXC (800 + 5) #define MAXH (100 + 5) #define rev(a) ((a) ^ 1) #define ca(i) (90000 + i) #define bh(i, j) ((i) * MAXC + (j)) #define cint const int #define rint register int const int s = 0, t = MAXN - 1; using namespace std; struct edg { int from, to, next, cap, flow, cost; edg() {} edg(cint fr, cint dqt, cint ne, cint ca, cint co): from(fr), to(dqt), next(ne), cap(ca), flow(0), cost(co) {} }b[MAXM]; int g[MAXN], cntb = -1, n, m, cnt[MAXC], dqch, ans, fans, gg[MAXH][MAXC], totp, pre[MAXN]; inline void adn(cint from, cint to, cint cap, cint cost) { b[++cntb] = edg(from, to, g[from], cap, cost); g[from] = cntb; } inline int min(const int x, const int y) { if (x > y) return y; else return x; } int spfa() { int dis[MAXN], a[MAXN]; bool inq[MAXN]; queue<int> q; memset(dis, 0x7f, sizeof(dis)); memset(inq, false, sizeof(inq)); a[t] = 0; a[s] = INF; dis[s] = 0; q.push(s); while (!q.empty()) { int dq = q.front(); q.pop(); inq[dq] = false; for (rint i = g[dq]; ~i; i = b[i].next) if (dis[b[i].to] > dis[dq] + b[i].cost && b[i].cap > b[i].flow) { dis[b[i].to] = dis[dq] + b[i].cost; a[b[i].to] = min(a[dq], b[i].cap - b[i].flow); pre[b[i].to] = i; if(!inq[b[i].to]) q.push(b[i].to), inq[b[i].to] = true; } } return a[t]; } int solve() { cint zl = spfa(); fans += zl; for (rint i = t; i != s; i = b[pre[i]].from) { if (b[pre[i]].from < 90000 && b[pre[i]].to == t) dqch = b[pre[i]].from / MAXC; b[pre[i]].flow += zl, b[rev(pre[i])].flow -= zl; ans += zl * b[pre[i]].cost; } return dqch; } void qwq() { for (rint i = 1; i <= m; i++) for (rint j = 1; j <= n; j++) adn(ca(j), bh(i, cnt[i] + 1), 1, gg[i][j] * (cnt[i] + 1)), adn(bh(i, cnt[i] + 1), ca(j), 0, -gg[i][j] * (cnt[i] + 1)); for (int i = 1; i <= m; i++) adn(bh(i, cnt[i] + 1), t, 1, 0), adn(t, bh(i, cnt[i] + 1), 0, 0);
while (fans < totp) { cint i = solve(); ++cnt[i]; for (rint j = 1; j <= n; j++) adn(ca(j), bh(i, cnt[i] + 1), 1, gg[i][j] * (cnt[i] + 1)), adn(bh(i, cnt[i] + 1), ca(j), 0, -gg[i][j] * (cnt[i] + 1)); adn(bh(i, cnt[i] + 1), t, 1, 0), adn(t, bh(i, cnt[i] + 1), 0, 0); } } inline int read() { rint re = 0, f = 1; register char x = 0; while (x < '0' || x > '9') { if (x == '-') f = -1; x = getchar(); } while (x >= '0' && x <= '9') re = (re << 1) + (re << 3) + x - '0', x = getchar(); return re * f; } int main() { memset(g, -1, sizeof(g));
n = read(), m = read(); for (rint i = 1, srx; i <= n; i++) srx = read(), adn(s, ca(i), srx, 0), adn(ca(i), s, 0, 0), totp += srx; for (rint i = 1; i <= n; i++) for (rint j = 1; j <= m; j++) gg[j][i] = read(); qwq(); printf("%d", ans); return 0; }
|