-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushRelabelAlgo.cpp
More file actions
162 lines (154 loc) · 4.19 KB
/
Copy pathPushRelabelAlgo.cpp
File metadata and controls
162 lines (154 loc) · 4.19 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <bits/stdc++.h>
using namespace std;
#define int long long
class Node{
private:
int index;
int h = 0;
int ex = 0;
vector<pair<int, int>> edges;
public:
Node(int i){
index = i;
}
int getIndex(){
return index;
}
int getHeight(){
return h;
}
int getExcess(){
return ex;
}
vector<pair<int, int>> getEdges(){
return edges;
}
void addHeight(int he){
h += he;
}
void addExcess(int e){
ex += e;
}
void addEdge(int node, int cap){
edges.push_back(make_pair(node, cap));
}
void removeEdge(int node){
for(auto it = edges.begin(); it != edges.end(); ++it){
if((*it).first == node) {
edges.erase(it);
return;
}
}
}
void clearEdges(){
edges.clear();
}
};
class CompareHeight{
public:
bool operator()(Node* n1, Node* n2){
if(n1->getHeight() < n2->getHeight()) return true;
return false;
}
};
class maxFlow{
private:
vector<Node*> nodes;
priority_queue<Node*, vector<Node*>, CompareHeight> nodeHeights;
int s;
int t;
public:
maxFlow(vector<Node*> n, int start, int dest){
nodes = n;
s = start;
t = dest;
}
void initPref();
void push(int a, int b);
void relabel(int a);
int solve();
};
void maxFlow::initPref(){
for(int i = 0; i < nodes.size(); i++){
if(i == s) nodes[i]->addHeight(nodes.size());
else nodes[i]->addHeight(0);
if(i != s && i != t) nodeHeights.push(nodes[i]);
}
vector<pair<int,int>> v = nodes[s]->getEdges();
for(int i = 0; i < v.size(); i++){
int index = v[i].first;
int cap = v[i].second;
nodes[index]->addEdge(s, cap);
nodes[index]->addExcess(cap);
nodes[s]->addExcess(-cap);
}
nodes[s]->clearEdges();
}
void maxFlow::push(int a, int b){
cout << "Pushing from " << a << " to " << b << endl;
vector<pair<int,int>> v = nodes[a]->getEdges();
int cap = 0;
for(int i = 0; i < v.size(); i++){
if(v[i].first == b) cap = v[i].second;
}
int f = min(nodes[a]->getExcess(), cap);
nodes[a]->removeEdge(b);
cap -= f;
int cap2 = 0;
v = nodes[b]->getEdges();
for(int i = 0; i < v.size(); i++){
if(v[i].first == a) cap2 = v[i].second;
}
nodes[b]->removeEdge(a);
cap2 += f;
nodes[b]->addExcess(f);
nodes[a]->addExcess(-f);
if(cap > 0) nodes[a]->addEdge(b, cap);
if(cap2 > 0) nodes[b]->addEdge(a, cap2);
if(nodes[a]->getExcess() > 0) nodeHeights.push(nodes[a]);
if(nodes[b]->getExcess() > 0 && b != t) nodeHeights.push(nodes[b]);
}
void maxFlow::relabel(int a){
cout << "Relabeling node " << a << endl;
nodes[a]->addHeight(1);
nodeHeights.push(nodes[a]);
}
int maxFlow::solve(){
initPref();
while(!nodeHeights.empty()){
Node* curr = nodeHeights.top();
nodeHeights.pop();
if(curr->getExcess() == 0) continue;
vector<pair<int,int>> v = curr->getEdges();
int i;
for(i = 0; i < v.size(); i++){
if(nodes[v[i].first]->getHeight() < curr->getHeight()){
push(curr->getIndex(), nodes[v[i].first]->getIndex());
break;
}
}
if(i == v.size()){
relabel(curr->getIndex());
}
}
return -nodes[s]->getExcess();
}
int32_t main(){
// s-3>a-2>t;
int n;
cin >> n;
vector<Node*> nodes;
for(int i = 0; i < n; i++){
Node* curr = new Node(i);
nodes.push_back(curr);
}
int m;
cin >> m;
int l, r, c;
for(int i = 0; i < m; i++){
cin >> l >> r >> c;
nodes[l - 1]->addEdge(r - 1, c);
}
maxFlow* mf = new maxFlow(nodes, 0, n-1);
cout << "maxflow = "<< mf->solve() << endl;
}