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
| #include <bits/stdc++.h> using LL = long long; const int N = 500 + 50, K = 50 + 5, INF = 0x3f3f3f3f; int n, m, S, T, tot, num; struct Edge{ int ne, ver, w; } eb[(N * N) << 2], e[(N * N) << 2]; int hb[N * N], idb = 0, h[N * N], idx = 0; int q[N * N], hh, tt; int dep[N * N], cur[N * N]; int id(int x, int y){ return (x - 1) * m + y; } void add(int x, int y, int z){ eb[idb] = {hb[x], y, z}, hb[x] = idb++; } void dad(int x, int y, int z){ add(x, y, z), add(y, x, z); } void add2(int x, int y, int z){ e[idx] = {h[x], y, z}, h[x] = idx++; } void dad2(int x, int y, int z){ add2(x, y, z), add2(y, x, z); } bool bfs() { std::memset(dep, 0, sizeof dep); q[hh = tt = 1] = S, cur[S] = h[S], dep[S] = 1; for (int x; hh <= tt; ) { x = q[hh++]; for (int i = h[x], y; ~i; i = e[i].ne) if (e[i].w && !dep[y = e[i].ver]) { dep[y] = dep[x] + 1, cur[y] = h[y]; if (y == T) return true; q[++tt] = y; } } return false; } int find(int x, int lim) { if (x == T) return lim; int flow = 0, t; for (int i = cur[x], y; ~i && flow < lim; i = e[i].ne) { cur[x] = i; if (e[i].w && dep[y = e[i].ver] == dep[x] + 1) { t = find(y, std::min(lim - flow, e[i].w)); if (!t) dep[y] = -1; else e[i].w -= t, e[i ^ 1].w += t, flow += t; } } return flow; } LL dinic() { LL res = 0; int flow; while (bfs()) while (flow = find(S, INF)) res += flow; return res; } int main() { scanf("%d %d %d",&n, &m, &num); std::memset(hb, -1, sizeof hb); std::memset(h, -1, sizeof h); for (int i = 1, w; i < n; ++i) for (int j = 1; j <= m; ++j) { scanf("%d", &w); dad(id(i, j), id(i + 1, j), w); } for (int i = 1, w; i <= n; ++i) for (int j = 1; j < m; ++j) { scanf("%d", &w); dad(id(i, j), id(i, j + 1), w); } S = id(n, m) + 1, tot = T = S + 1; for (int k, w, p, col; num--; ) { scanf("%d", &k); std::copy(hb + 1, hb + tot + 1, h + 1); std::copy(eb, eb + idb, e); idx = idb, tot = T; for (int i = 1; i <= k; ++i) { scanf("%d %d %d", &w, &p, &col); ++tot; col ? dad2(S, tot, INF) : dad2(tot, T, INF); if (p <= m) dad2(id(1, p), tot, w); else if (p <= m + n) dad2(id(p - m, m), tot, w); else if (p <= (m << 1) + n) dad2(id(n, m - (p - m - n) + 1), tot, w); else dad2(id(n - (p - m - m - n) + 1, 1), tot, w); } printf("%lld\n", dinic()); } return 0; }
|