-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdmond-Karp.cpp
More file actions
115 lines (108 loc) · 2.86 KB
/
Edmond-Karp.cpp
File metadata and controls
115 lines (108 loc) · 2.86 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
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>
using namespace std;
#define int long long
class maxFlow{
private:
int n;
vector<vector<pair<int,int>>> adj;
int s;
int t;
int maxflow = 0;
public:
maxFlow(int no, vector<vector<pair<int,int>>> edges, int start, int dest){
adj = edges;
n = no;
s = start;
t = dest;
}
vector<int> shortestPath();
void push(vector<int> path);
int getCapacity(int a, int b);
void changeEdge(int a, int b, int w);
int solve();
};
vector<int> maxFlow :: shortestPath(){
queue<int> BFS;
vector<int> visited(n, -1);
BFS.push(s);
visited[s] = 1;
while(!BFS.empty()){
int curr = BFS.front();
BFS.pop();
//cout << "current node : " << curr << endl;
for(auto it : adj[curr]){
int k = it.first;
if(visited[k] == -1){
visited[k] = curr;
BFS.push(k);
}
}
}
int it = t;
vector<int> path;
if(visited[t] == -1) return path;
while(it != s){
path.push_back(it);
it = visited[it];
}
path.push_back(s);
reverse(path.begin(), path.end());
return path;
}
int maxFlow :: getCapacity(int a, int b){
for(auto it : adj[a]){
if(it.first == b) return it.second;
}
return 0;
}
void maxFlow :: push(vector<int> path){
//cout << "entered" << endl;
int capacity = INT_MAX;
for(int i = 0; i < path.size() - 1; i++){
capacity = min(capacity, getCapacity(path[i], path[i + 1]));
}
//cout << "capacity" << capacity << endl;
maxflow += capacity;
for(int i = 0; i < path.size() - 1; i++){
changeEdge(path[i], path[i + 1], -capacity);
changeEdge(path[i + 1], path[i], capacity);
}
}
void maxFlow :: changeEdge(int a, int b, int w){
for(auto it = adj[a].begin(); it != adj[a].end(); ++it){
if((*it).first == b) {
((*it).second) += w;
if((*it).second == 0){
adj[a].erase(it);
}
return;
}
}
adj[a].push_back(make_pair(b, w));
}
int maxFlow :: solve(){
vector<int> path = {0};
while(1){
//cout << "Finding path" << endl;
path = shortestPath();
if(path.empty()) break;
push(path);
//cout << "Push Successful" << endl;
}
return maxflow;
}
int32_t main(){
// s-3>a-2>t;
int n;
cin >> n;
vector<vector<pair<int,int>>> adj(n);
int m;
cin >> m;
int l, r, c;
for(int i = 0; i < m; i++){
cin >> l >> r >> c;
adj[l].push_back(make_pair(r, c));
}
maxFlow* mf = new maxFlow(n, adj, 0, n - 1);
cout << "maxflow = "<< mf->solve() << endl;
}