-
Notifications
You must be signed in to change notification settings - Fork 122
/
Edmonds_Karp.cpp
87 lines (80 loc) · 2.28 KB
/
Edmonds_Karp.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
// Maximum Flow (Dinic Algorithm)
// Time complexity: O(n^2m)
// A slight improvement from Edmonds_Karp Algorithm
// Problem link: https://cses.fi/problemset/task/1694/
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
struct max_flow_graph {
struct edge {
int u, v, cap, flow;
};
int n;
vector<edge> el;
vector<vector<int>> adj;
vector<int> dist, par;
max_flow_graph(int n) : n(n), adj(n) {}
void add_edge(int u, int v, int w) {
adj[u].push_back(el.size());
el.push_back({u, v, w, 0});
adj[v].push_back(el.size());
el.push_back({v, u, 0, 0});
}
int send_one_flow(int s, int e) {
int nf = INF;
for (int u = e; u != s; u = el[par[u]].u) {
nf = min(nf, el[par[u]].cap - el[par[u]].flow);
}
for (int u = e; u != s; u = el[par[u]].u) {
el[par[u]].flow += nf;
el[par[u]^1].flow -= nf;
}
return nf;
}
bool bfs(int s, int e) {
dist.assign(n, INF);
par.assign(n, 0);
queue<int> q;
q.push(s); dist[s] = 0;
while (q.size()) {
int u = q.front(); q.pop();
if (u == e) break;
for (int idx : adj[u]) {
if (el[idx].cap > el[idx].flow && dist[el[idx].v] > dist[el[idx].u] + 1) {
dist[el[idx].v] = dist[el[idx].u] + 1;
par[el[idx].v] = idx;
q.push(el[idx].v);
}
}
}
return dist[e] < INF;
}
ll edmonds_karp(int s, int e) {
ll mf = 0;
while (bfs(s, e)) mf += send_one_flow(s, e);
return mf;
}
};
void solve() {
int n, m; cin >> n >> m;
max_flow_graph adj(n);
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w; u--; v--;
adj.add_edge(u, v, w);
}
cout << adj.edmonds_karp(0, n - 1) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}