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
| #include <cstdio> #include <queue> #include <cstring>
const int INF = 1e9; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; inline int min(const int x, const int y) {return x < y ? x : y;} struct Edge { int to, nxt, cap; } e[1000005]; int head[100005], cur[100005], dep[100005], num[100005], s, t, tot = 1, cnt; int ar[105][105], sc[105][105]; std::queue<int> Q; inline void AddEdge(const int u, const int v, const int cap) { e[++ tot].to = v, e[tot].cap = cap, e[tot].nxt = head[u], head[u] = tot; e[++ tot].to = u, e[tot].cap = 0, e[tot].nxt = head[v], head[v] = tot; }
void bfs() { memcpy(cur, head, sizeof cur); num[dep[t] = 1] = 1; Q.push(t); while (Q.size()) { int u(Q.front()); Q.pop(); for (int i(head[u]); i; i = e[i].nxt) if (!dep[e[i].to]) ++ num[dep[e[i].to] = dep[u] + 1], Q.push(e[i].to); } }
int dfs(const int u, const int flow) { if (u == t) return flow; int used(0), tmp(0); for (int i(cur[u]); i; i = e[i].nxt) { cur[u] = i; if (e[i].cap && dep[u] - 1 == dep[e[i].to]) { tmp = dfs(e[i].to, min(e[i].cap, flow - used)); e[i].cap -= tmp, e[i ^ 1].cap += tmp, used += tmp; if (used == flow) return used; } } cur[u] = head[u]; if (!(-- num[dep[u]])) dep[s] = cnt + 2; ++ num[++ dep[u]]; return used; }
int ISAP() { int Maxflow(0); bfs(); while (dep[s] <= cnt + 1) Maxflow += dfs(s, INF); return Maxflow; }
int main() { int n, m, ans(0); scanf("%d%d", &n, &m); s = 0, t = (cnt = n * m + 1); for (int i(1); i <= n; ++ i) for (int j(1); j <= m; ++ j) { int x; scanf("%d", &x); AddEdge(s, (i - 1) * m + j, x), ans += x; } for (int i(1); i <= n; ++ i) for (int j(1); j <= m; ++ j) { int x; scanf("%d", &x); AddEdge((i - 1) * m + j, t, x), ans += x; } for (int i(1); i <= n; ++ i) for (int j(1); j <= m; ++ j) { int x, u(++ cnt); scanf("%d", &x); AddEdge(s, u, x), ans += x; AddEdge(u, (i - 1) * m + j, INF); for (int k(0); k <= 3; ++ k) { int tx(i + dx[k]), ty(j + dy[k]); if (tx < 1 || ty < 1 || n < tx || m < ty) continue; AddEdge(u, (tx - 1) * m + ty, INF); } } for (int i(1); i <= n; ++ i) for (int j(1); j <= m; ++ j) { int x, u(++ cnt); scanf("%d", &x); AddEdge(u, t, x), ans += x; AddEdge((i - 1) * m + j, u, INF); for (int k(0); k <= 3; ++ k) { int tx(i + dx[k]), ty(j + dy[k]); if (tx < 1 || ty < 1 || n < tx || m < ty) continue; AddEdge((tx - 1) * m + ty, u, INF); } } printf("%d", ans - ISAP()); }
|