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