-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdinic.cpp
91 lines (77 loc) · 1.55 KB
/
dinic.cpp
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
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000000000007
using namespace std;
const ll nodes = 1002;
vector<vector<ll>> adj(nodes);
vector<vector<ll>> capacity(nodes, vector<ll>(nodes)), flow(nodes, vector<ll>(nodes));
vector<ll> dist, pt;
queue<ll> q;
ll src, dest;
void add_edge(ll u, ll v, ll c) {
if (u != v) {
adj[u].push_back(v);
adj[v].push_back(u);
capacity[u][v] += c;
capacity[v][u] += c;
}
}
bool bfs() {
fill(dist.begin(), dist.end(),INF);
dist[src] = 0;
q.push(src);
while (!q.empty() and dist[dest] == INF) {
ll u = q.front(); q.pop();
for (ll to : adj[u]) {
if (dist[to] == INF and flow[u][to] < capacity[u][to]) {
dist[to] = dist[u] + 1;
q.push(to);
}
}
}
while (!q.empty()) q.pop();
return dist[dest] != INF;
}
ll dfs(ll u, ll curr_flow) {
if (curr_flow == 0 or u == dest) {
return curr_flow;
}
for (; pt[u] < adj[u].size(); pt[u]++) {
ll to = adj[u][pt[u]];
if (dist[to] == dist[u] + 1) {
ll pushed = dfs(to, min(curr_flow, capacity[u][to] - flow[u][to]));
if (pushed > 0) {
flow[u][to] += pushed;
flow[to][u] -= pushed;
return pushed;
}
}
}
return 0;
}
ll dinic() {
ll flow = 0;
while (bfs()) {
fill(pt.begin(), pt.end(),0);
while (ll pushed = dfs(src, INF)) {
flow += pushed;
}
}
return flow;
}
int main() {
ll n,m,i,u,v,c;
cin >> n >> m;
adj.resize(n);
dist.resize(n);
pt.resize(n);
for (i = 0; i < m; i++) {
cin >> u >> v >> c;
u--;v--;
add_edge(u,v,c);
}
cin >> src >> dest;
src--;dest--;
cout << dinic() << endl;
return 0;
}