最大流最小割模板题。

想当年第一次打开 BZOJ 做完 A + B 之后看得一脸懵逼的就是这道题,没想到现如今看起来这么简单

不过还是没有秒切,双向边没看到调了好久 QAQ

好了,关于那个连边。这是本人单向边网络流连边:

1
2
3
4
5
inline void add_edge(int u, int v, int w) {
nxt[tot] = hed[u], to[tot] = v, val[tot] = w, hed[u] = tot++;
nxt[tot] = hed[v], to[tot] = u, val[tot] = 0, hed[v] = tot++;
return;
}

这是本人的双向网络流连边(连两遍也是一样的):

1
2
3
4
5
inline void add_edge(int u, int v, int w) {
nxt[tot] = hed[u], to[tot] = v, val[tot] = w, hed[u] = tot++;
nxt[tot] = hed[v], to[tot] = u, val[tot] = w, hed[v] = tot++;
return;
}

另外 $最大流 = 最小割$ 不必多说了吧,贴个直观的证明(来自:https://jecvay.com/2014/11/what-is-min-cut.html):

1.最大流不可能大于最小割, 因为最大流所有的水流都一定经过最小割那些割边, 流过的水流怎么可能比水管容量还大呢?

2.最大流不可能小于最小割, 如果小, 那么说明水管容量没有物尽其用, 可以继续加大水流.

那么 SAP + 当前弧优化 + 断层优化 + 反向 BFS 一遍轻松跑过。

代码:

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
// ==============================
// author: memset0
// website: https://memset0.cn
// ==============================
#include <bits/stdc++.h>
#define ll long long
#define rep(i,l,r) for (int i = l; i <= r; i++)
#define getc(x) getchar(x)
#define putc(x) putchar(x)

template <typename T> inline void read(T &x) {
x = 0; register char ch; register bool fl = 0;
while (ch = getc(), ch < 48 || 57 < ch) fl ^= ch == '-'; x = (ch & 15);
while (ch = getc(), 47 < ch && ch < 58) x = (x << 1) + (x << 3) + (ch & 15);
if (fl) x = -x;
}
template <typename T> inline void print(T x, char c = '\n') {
static int buf[40];
if (x == 0) { putc('0'); return; }
if (x < 0) putc('-'), x = -x;
for (buf[0] = 0; x; x /= 10) buf[++buf[0]] = x % 10 + 48;
while (buf[0]) putc((char) buf[buf[0]--]);
putc(c);
}

const int maxn = 1000010, maxm = maxn * 6, inf = 1e9;

int n, m, x, u, l, r, ans, flow, flag;
int pre[maxn], dep[maxn], gap[maxn], cur[maxn], q[maxn];
int tot = 2, hed[maxn], to[maxm], val[maxm], nxt[maxm];

#define at(i,j) (((i) - 1) * m + (j))

inline void add_edge(int u, int v, int w) {
nxt[tot] = hed[u], to[tot] = v, val[tot] = w, hed[u] = tot++;
nxt[tot] = hed[v], to[tot] = u, val[tot] = w, hed[v] = tot++;
return;
}

void bfs() {
for (int i = 1; i <= n * m; i++)
cur[i] = hed[i];
l = r = 1, q[1] = n * m, gap[1] = 1, dep[n * m] = 1;
while (l <= r) {
u = q[l++];
for (int i = hed[u], v = to[i]; i; i = nxt[i], v = to[i]) {
if (!dep[v]) {
dep[v] = dep[u] + 1;
gap[dep[v]]++;
q[++r] = v;
}
}
}
}

void isap(int s, int e) {
u = s;
while (dep[s] <= e) {
flag = 0;
for (int &i = cur[u], v = to[i]; i; i = nxt[i], v = to[i])
if (val[i] && dep[v] + 1 == dep[u]) {
u = v;
pre[v] = i;
flag = 1;
break;
}
if (!flag) {
if (!--gap[dep[u]]) break;
dep[u] = e;
for (int i = hed[u], v = to[i]; i; i = nxt[i], v = to[i])
if (val[i] && dep[v] + 1 < dep[u]) {
dep[u] = dep[v] + 1;
cur[u] = i;
}
++gap[dep[u]];
if (u ^ s) u = to[pre[u] ^ 1];
}
if (u == e) {
flow = inf;
for (int i = pre[e]; i; i = pre[to[i ^ 1]])
flow = std::min(flow, val[i]);
for (int i = pre[e]; i; i = pre[to[i ^ 1]])
val[i] -= flow, val[i ^ 1] += flow;
ans += flow, u = s;
}
}
}

int main() {

read(n), read(m);
for (int i = 1; i <= n; i++)
for (int j = 1; j < m; j++)
read(x), add_edge(at(i, j), at(i, j + 1), x);
for (int i = 1; i < n; i++)
for (int j = 1; j <= m; j++)
read(x), add_edge(at(i, j), at(i + 1, j), x);
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
read(x), add_edge(at(i, j), at(i + 1, j + 1), x);

bfs();
isap(1, n * m);
print(ans);

return 0;
}