forked from spectre900/Binary-Image-Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edmondKarp.cpp
91 lines (76 loc) · 1.67 KB
/
edmondKarp.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
#include <bits/stdc++.h>
#include "graph.h"
using namespace std;
class EdmondKarp : public Graph
{
public:
int bfs()
{
int flow = INT_MAX;
vector<int> parent(V, -1);
queue<int> q;
q.push(source);
while (!q.empty())
{
int u = q.front();
q.pop();
for (pair<int, int> edge : adjList[u])
{
int v = edge.first;
int w = edge.second;
if (v != source and parent[v] == -1 and w > 0)
{
q.push(v);
parent[v] = u;
}
}
}
if (parent[sink] == -1)
return 0;
int v = sink;
int u = parent[v];
while (u != -1)
{
flow = min(flow, adjList[u][v]);
v = u;
u = parent[u];
}
v = sink;
u = parent[v];
while (u != -1)
{
adjList[u][v] -= flow;
adjList[v][u] += flow;
v = u;
u = parent[u];
}
return flow;
}
int findMaxFlow()
{
while (true)
{
int flow = bfs();
if (flow)
{
maxFlow += flow;
done = vector<bool>(V, false);
continue;
}
break;
}
return maxFlow;
}
};
int main()
{
EdmondKarp ek = EdmondKarp();
ek.readInput();
clock_t start, end;
start = clock();
ek.findMaxFlow();
ek.findForeGround();
end = clock();
double duration = double(end - start) / double(CLOCKS_PER_SEC);
cout << duration;
}