注意事项:

  1. 变量 cntnex 初值要设为奇数。

Ek 算法

代码;

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
int l, r, que[MAXn + 10];
bool vis[MAXn + 10]; int prec[MAXn + 10], lim[MAXn + 10];
bool bfs(int s, int t) {
l = 1, r = 0;
memset(vis, 0, sizeof(vis));
que[++r] = s;
vis[s] = 1;
prec[s] = 0;
lim[s] = INF;
while (l <= r) {
int cur = que[l++];
for (int i = head[cur]; i; i = nex[i]) {
if (cap[i] == 0 || vis[to[i]]) continue;
que[++r] = to[i];
vis[to[i]] = 1;
prec[to[i]] = i;
lim[to[i]] = min(lim[cur], cap[i]);
if (to[i] == t) return 1;
}
}
return 0;
}

void ek(int s, int t, int &ansf) {
ansf = 0;
while (bfs(s, t)) {
ansf += lim[t];
int cur = t;
while (cur != s) {
cap[prec[cur]] -= lim[t]; cap[prec[cur] ^ 1] += lim[t];
cur = to[prec[cur] ^ 1];
}
}
}

Dinic 算法

代码;

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
int arc[MAXn + 10], lay[MAXn + 10];
int l, r, que[MAXn + 10];
bool bfs(int s, int t) {
l = 1, r = 0;
memset(lay, -1, sizeof(lay));
arc[s] = head[s];
lay[s] = 0;
que[++r] = s;
while (l <= r) {
int cur = que[l++];
for (int i = head[cur]; i; i = nex[i]) {
if (cap[i] == 0 || lay[to[i]] != -1) continue;
arc[to[i]] = head[to[i]];
lay[to[i]] = lay[cur] + 1;
if (to[i] == t) return 1;
que[++r] = to[i];
}
}
return 0;
}
int dfs(int cur, int t, int lim) {
if (cur == t) return lim;
int ans = 0;
for (int i = arc[cur]; i && ans < lim; i = nex[i]) {
arc[cur] = i;
if (cap[i] == 0 || lay[to[i]] != lay[cur] + 1) continue;
int f = dfs(to[i], t, min(lim - ans, cap[i]));
if (f == 0) lay[to[i]] = -1;
else {
ans += f;
cap[i] -= f; cap[i ^ 1] += f;
}
}
return ans;
}
void dinic(int s, int t, int &ansf) {
ansf = 0;
while (bfs(s, t)) ansf += dfs(s, t, INF);
}

题目

Luogu P3376 【模板】网络最大流

代码(以 Dinic 为例):

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
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXn = 2e2;
const int MAXm = 5e3;
const int INF = 0x3f3f3f3f;

template <typename T>
inline void read(T &a) {
char c;for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar());bool f = c == '-';T x = f ? 0 : (c ^ '0');for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) {x = x * 10 + (c ^ '0');}a = f ? -x : x;
}
template <typename T, typename ...Argv>
inline void read(T &a, Argv &...argv) {
read(a), read(argv...);
}

int head[MAXn + 10], cntnex = 1, nex[MAXm * 2 + 10], to[MAXm * 2 + 10], cap[MAXm * 2 + 10];
inline void connect(int u, int v, int c) {
nex[++cntnex] = head[u];
head[u] = cntnex;
to[cntnex] = v;
cap[cntnex] = c;
}

int arc[MAXn + 10], lay[MAXn + 10];
int l, r, que[MAXn + 10];
bool bfs(int s, int t) {
l = 1, r = 0;
memset(lay, -1, sizeof(lay));
arc[s] = head[s];
lay[s] = 0;
que[++r] = s;
while (l <= r) {
int cur = que[l++];
for (int i = head[cur]; i; i = nex[i]) {
if (cap[i] == 0 || lay[to[i]] != -1) continue;
arc[to[i]] = head[to[i]];
lay[to[i]] = lay[cur] + 1;
if (to[i] == t) return 1;
que[++r] = to[i];
}
}
return 0;
}
int dfs(int cur, int t, int lim) {
if (cur == t) return lim;
int ans = 0;
for (int i = arc[cur]; i && ans < lim; i = nex[i]) {
arc[cur] = i;
if (cap[i] == 0 || lay[to[i]] != lay[cur] + 1) continue;
int f = dfs(to[i], t, min(lim - ans, cap[i]));
if (f == 0) lay[to[i]] = -1;
else {
ans += f;
cap[i] -= f; cap[i ^ 1] += f;
}
}
return ans;
}
void dinic(int s, int t, int &ansf) {
ansf = 0;
while (bfs(s, t)) ansf += dfs(s, t, INF);
}

int n, m, s, t;
signed main() {
read(n, m, s, t);
for (int i = 1, u, v, c; i <= m; ++i) {
read(u, v, c);
connect(u, v, c); connect(v, u, 0);
}
int ansf;
dinic(s, t, ansf);
printf("%lld\n", ansf);
return 0;
}