-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushRelabelCPU.cpp
346 lines (311 loc) · 6.31 KB
/
pushRelabelCPU.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <algorithm>
#include <iostream>
#include <list>
#include <limits.h>
#include <queue>
#define FLATIMAGESIZE 262 * 197
using namespace std;
struct Edge
{
bool used;
int v;
long long capacity;
Edge(int v = -1, long long capacity = -1, bool used = false)
{
this -> v = v;
this -> capacity = capacity;
this -> used = false;
}
};
long long sourceEdges[FLATIMAGESIZE + 10], sinkEdges[FLATIMAGESIZE + 10];
long long sourceEdgesCpy[FLATIMAGESIZE + 10], sinkEdgesCpy[FLATIMAGESIZE + 10];
Edge edges[FLATIMAGESIZE + 10][10];
class Graph
{
public:
list<int> *adj;
bool *active, *reachable;
int *labelCount, *label;
long long *excess;
queue<int> activeVertices;
int source, sink, V;
Graph(int V);
void setTerminals(int source, int sink);
void addEdge(int u, int v, long long capacity);
void BFS();
void initializePreflow();
void markActive(int u);
void relabel(int u);
void push(int u, int v);
void gap(int k);
long long result();
};
Graph::Graph(int V)
{
this -> V = V;
label = new int[V];
labelCount = new int[2 * V];
excess = new long long[V];
active = new bool[V];
reachable = new bool[V];
adj = new list<int>[V];
}
void Graph::setTerminals(int source, int sink)
{
this -> source = source;
this -> sink = sink;
}
void Graph::addEdge(int u, int v, long long capacity)
{
adj[u].push_back(v);
if (u == this -> source)
{
if (sourceEdges[v] < 0)
{
sourceEdges[v] = capacity;
sourceEdgesCpy[v] = capacity;
}
else
{
sourceEdges[v] += capacity;
sourceEdgesCpy[v] += capacity;
}
}
else if (u == this -> sink)
{
if (sinkEdges[v] < 0)
{
sinkEdges[v] = capacity;
sinkEdgesCpy[v] = capacity;
}
else
{
sinkEdges[v] += capacity;
sinkEdgesCpy[v] += capacity;
}
}
else
{
bool flag = false;
int pos = -1;
for (int i = 0; i < 10; i++)
{
if (edges[u][i].v == v)
{
flag = true;
edges[u][i].capacity += capacity;
}
else if (edges[u][i].capacity == -1)
{
pos = i;
break;
}
}
if (!flag)
{
edges[u][pos].v = v;
edges[u][pos].capacity = capacity;
}
}
}
void Graph::BFS()
{
queue<int> neighbours;
list<int>::iterator iter;
reachable[this -> source] = true;
neighbours.push(this -> source);
while (!neighbours.empty())
{
int x = neighbours.front(), y, i;
neighbours.pop();
for (iter = adj[x].begin(); iter != adj[x].end(); iter++)
{
y = *iter;
if (!reachable[y])
{
if (x == this -> source && sourceEdges[y] > 0 && sourceEdges[y] < sourceEdgesCpy[y])
{
cout << y << '\n';
reachable[y] = true;
neighbours.push(y);
}
else if (x != this -> sink && x != this -> source)
for (i = 0; i < 10; i++)
if (edges[x][i].v == y && edges[x][i].capacity > 0 && edges[x][i].used)
{
cout << x << ' ' << y << ' ' << edges[x][i].capacity << '\n';
reachable[y] = true;
neighbours.push(y);
}
}
}
}
}
void Graph::initializePreflow()
{
for (int i = 0; i < this -> V; i++)
{
active[i] = false;
excess[i] = 0;
label[i] = 0;
labelCount[2 * i] = 0;
labelCount[2 * i + 1] = 0;
}
label[this -> source] = this -> V;
labelCount[0] = this -> V - 1;
labelCount[this -> V] = 1;
for (int i = 0; i < this -> V; i++)
if (sourceEdges[i] >= 0)
excess[this -> source] += sourceEdges[i];
}
void Graph::markActive(int u)
{
if (!active[u] && excess[u] > 0)
{
active[u] = true;
activeVertices.push(u);
}
}
void Graph::gap(int k)
{
for (int i = 0; i < this -> V; i++)
{
if (label[i] < k)
continue;
labelCount[label[i]]--;
label[i] = max(label[i], this -> V + 1);
labelCount[label[i]]++;
markActive(i);
}
}
void Graph::relabel(int u)
{
labelCount[label[u]]--;
int i, minLabel = INT_MAX;
// Source and sink are never relabeled.
for (i = 0; i < 10; i++)
if (edges[u][i].capacity > 0)
{
minLabel = min(minLabel, label[edges[u][i].v]);
label[u] = minLabel + 1;
}
labelCount[label[u]]++;
markActive(u);
}
void Graph::push(int u, int v)
{
long long diff = excess[u];
int i, pos = -1;
if (u == this -> source && sourceEdges[v] > 0)
diff = min(diff, sourceEdges[v]);
else if (u == this -> sink && sinkEdges[v] > 0)
diff = min(diff, sinkEdges[v]);
else if (u != this -> source && u != this -> sink)
for (i = 0; i < 10; i++)
if (edges[u][i].v == v)
{
pos = i;
diff = min(diff, edges[u][i].capacity);
}
if (diff == 0 || label[u] <= label[v])
return;
excess[u] -= diff;
excess[v] += diff;
if (u == this -> source)
{
sourceEdges[v] -= diff;
if (v == this -> sink)
sinkEdges[u] += diff;
else
for (i = 0; i < 10; i++)
if (edges[v][i].v == u)
{
edges[v][i].capacity += diff;
edges[v][i].used = true;
}
}
else if (u == this -> sink)
{
sinkEdges[v] -= diff;
if (v == this -> source)
sourceEdges[v] += diff;
else
for (i = 0; i < 10; i++)
if (edges[v][i].v == u)
{
edges[v][i].capacity += diff;
edges[v][i].used = true;
}
}
else
{
edges[u][pos].capacity -= diff;
edges[u][pos].used = true;
if (v == this -> source)
sourceEdges[u] += diff;
else if (v == this -> sink)
sinkEdges[u] += diff;
else
for (i = 0; i < 10; i++)
if (edges[v][i].v == u)
{
edges[v][i].capacity += diff;
edges[v][i].used = true;
}
}
markActive(v);
}
long long Graph::result()
{
long long maxFlow = 0;
initializePreflow();
for (int i = 0; i < this -> V; i++)
if (sourceEdges[i] >= 0)
push(this -> source, i);
long long count = 0;
while (!activeVertices.empty())
{
int u = activeVertices.front();
activeVertices.pop();
active[u] = false;
// cout << adj[u].size() << ' ';
if (u == source || u == sink)
continue;
for (int i = 0; i < 10; i++)
if (edges[u][i].capacity > 0)
push(u, edges[u][i].v);
if (excess[u] > 0)
{
if (labelCount[label[u]] == 1)
gap(label[u]);
else
relabel(u);
}
// cout << excess[activeVertices.front()] << ' ' << label[activeVertices.front()] << '\n';
count++;
}
return maxFlow = excess[sink];
}
int main()
{
int i, j, n, m, x, y, z;
list<int>::iterator iter;
for (i = 0; i < FLATIMAGESIZE + 10; i++)
{
sourceEdges[i] = -1;
sinkEdges[i] = -1;
}
cin >> n >> m;
Graph g(n);
g.setTerminals(0, n - 1);
while (m--)
{
cin >> x >> y >> z;
if (x != y && z > 0)
g.addEdge(x - 1, y - 1, z);
}
cout << g.result() << '\n';
g.BFS();
for (i = 0; i < n; i++)
cout << g.reachable[i] << ' ';
}