-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow.cpp
More file actions
108 lines (85 loc) · 1.93 KB
/
flow.cpp
File metadata and controls
108 lines (85 loc) · 1.93 KB
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
#include <bits/stdc++.h>
using namespace std;
#define MAINRET(x) in##x
#define LL long long
void solve();
MAINRET(t) main(void) {
std::cin.tie(nullptr);
std::cin.sync_with_stdio(false);
solve();
}
#define INF numeric_limits<LL>::max() / 2
const LL MX = 5 * 1e6;
//const LL MOD = 1e7;
LL N, s, t, m;
LL lvl[MX];
LL nextchld[MX];
LL start[MX];
LL succ[MX], cap[MX], to[MX];
LL edge_counter = 0;
bool bfs() {
for (LL i = 0; i < N; i++) lvl[i] = -1;
queue<LL> q;
q.push(s); lvl[s] = 0;
while (!q.empty()) {
LL u = q.front(); q.pop();
nextchld[u] = start[u];
for (LL e = start[u]; ~e; e = succ[e]) {
if (cap[e] > 0) {
LL nxt = to[e];
if (lvl[nxt] != -1) continue;
lvl[nxt] = lvl[u] + 1;
q.push(nxt);
}
}
}
return lvl[t] != -1;
}
LL aug(LL u, LL cflow) {
if (u == t) return cflow;
for (LL &i = nextchld[u]; i >= 0; i = succ[i]) {
if (cap[i] > 0) {
LL nxt = to[i];
if (lvl[nxt] != lvl[u] + 1) continue;
LL rf = aug(nxt, min(cflow, cap[i]));
if (rf > 0) {
cap[i] -= rf;
cap[i^1] += rf;
return rf;
}
}
}
lvl[u]=-1;
return 0;
}
LL mf() {
LL tot = 0;
while (bfs())
for (LL x = aug(s,INF); x; x = aug(s,INF)) tot+=x;
return tot;
}
void _add_edge(LL u, LL v, LL c) {
cap[edge_counter] = c, to[edge_counter] = v;
succ[edge_counter] = start[u];
start[u] = edge_counter;
++edge_counter;
}
void add_edge(LL u, LL v, LL c) {
_add_edge(u, v, c);
_add_edge(v, u, 0);
}
/*
*/
void solve() {
fill(start, start+MX, -1);
cin >> N >> m;
int a, b, c;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
a--; b--;
add_edge(a, b, c);
}
s = 0;
t = N-1;
cout << mf() << endl;
}