forked from chistopher/CPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedmonds_karp.cpp
More file actions
99 lines (80 loc) · 2.45 KB
/
edmonds_karp.cpp
File metadata and controls
99 lines (80 loc) · 2.45 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
//
// Created by Jeanne Aue on 14.06.24.
//
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <cstring>
#define REP(i, start, end) for(long long i = start; i < (end); i++)
#define append push_back
using namespace std;
using ll = long long;
typedef vector<int> vi;
struct edge {
ll from, to;
ll flow, cap;
edge* reverse;
};
//I do not want to write comments... my future self will hate me
bool bfs(vector<vector<edge *>>& graph, int s, int t, vector<edge *> &parent) {
fill(parent.begin(), parent.end(), nullptr);
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto e : graph[u]) {
if (!parent[e->to] && e->to != s && e->cap > e->flow) {
parent[e->to] = e;
q.push(e->to);
if (e->to == t) return true;
}
}
}
return false;
}
ll fordFulkerson(vector<vector<edge *>>& graph, int s, int t) {
ll max_flow = 0;
vector<edge *> parent(graph.size());
while (bfs(graph, s, t, parent)) {
// Find the maximum flow through the path found by BFS (two times find.... work on your language skills)
ll path_flow = LLONG_MAX;
for (edge *e = parent[t]; e != nullptr; e = parent[e->from]) {
path_flow = min(path_flow, e->cap - e->flow);
}
// Update the capacities of the edges and reverse edges along the path
for (edge *e = parent[t]; e != nullptr; e = parent[e->from]) {
e->flow += path_flow;
e->reverse->flow -= path_flow;
}
max_flow += path_flow;
}
return max_flow;
}
void add_edge_to_flow(vector<vector<edge *>>& graph, ll start, ll end, ll cap){
auto *forward = new edge{start, end, 0, cap, nullptr};
//works for undirected graphs, if direction is needed: start flow at 0
auto *reverse = new edge{end, start, 0, cap, forward};
forward->reverse = reverse;
graph[start].push_back(forward);
graph[end].push_back(reverse);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// Read the input
ll n, m;
cin >> n >> m;
vector<vector<edge *>> graph(n);
REP(i, 0, m) {
ll a, b, c;
cin >> a >> b >> c;
a--; b--;
add_edge_to_flow(graph, a,b,c);
}
// The source is always 0 and the sink is always 1
ll max_flow = fordFulkerson(graph, 0, 1);
cout << max_flow << endl;
return 0;
}