# 網絡流_洛谷P4001_最小割

1:(x,y)<==>(x+1,y)

2:(x,y)<==>(x,y+1)

3:(x,y)<==>(x+1,y+1)

3 4
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6

14

1.s 和 t 需要在 n, m初始化後再賦值
2. 最後要記得 flow = dinic(s, inf) ，別忘了給flow 賦值！！！

AC代碼：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int N = 1e6 + 5, M = 12000005;
const int inf = 0x3f3f3f3f;

int n, m, s, t, maxflow;
int h[N], w[M], ne[M], e[M], idx;
int d[N];

inline void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
e[idx] = a, w[idx] = 0, ne[idx] = h[b], h[b] = idx ++;
}

inline bool bfs(void) {
queue<int> q;
while(q.size()) q.pop();
memset(d, 0, sizeof d);

d[s] = 1, q.push(s);

while(q.size()) {
int u = q.front(); q.pop();

for(int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
if(w[i] && !d[v]) {
d[v] = d[u] + 1;
q.push(v);
}
}
}

if(d[t] == 0) return false;
return true;
}

inline int dinic(int u, int flow) {
if(u == t) return flow;

for(int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
if(d[v] == d[u] + 1 && w[i]) {
int k = dinic(v, min(flow, w[i]));
if(k) {
w[i] -= k;
w[i ^ 1] += k;
return k;
} else d[v] = 0;
}
}

return 0;
}
int main(void) {
//	freopen("in.txt", "r", stdin);

scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
s = 1, t = n * m;
for(int i = 1; i <= n; i ++) {
for(int j = 2; j <= m; j ++) {
int c; scanf("%d", &c);
int a = (i - 1) * m + j - 1;
int b = (i - 1) * m + j;
//			printf("%d %d %d\n", a, b, c);
}
}

for(int i = 2; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
int c; scanf("%d", &c);
int a = (i - 2) * m + j;
int b = (i - 1) * m + j;
//			printf("%d %d %d\n", a, b, c);
}
}

for(int i = 2; i <= n; i ++) {
for(int j = 2; j <= m; j ++) {
int c; scanf("%d", &c);
int a = (i - 2) * m + j - 1;
int b = (i - 1) * m + j;
//			printf("%d %d %d\n", a, b, c);
}
}

int flow;
while(bfs())
while(flow = dinic(s, inf)) maxflow += flow;

printf("%d\n", maxflow);

//	fclose(stdin);
return 0;
}