-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfordFulkerson2.cpp
121 lines (106 loc) · 1.73 KB
/
fordFulkerson2.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
struct MaxFlow
{
ll V, source, sink;
vector<map<ll,ll> > rG;
MaxFlow(ll v)
{
V = v+2;
source = v+1;
sink = v+2;
For(i,0,V+1)
{
map<ll,ll> t;
rG.pb(t);
}
}
void addEdge(ll u, ll v, ll w1, ll w2)
{
rG[u][v] = w1;
rG[v][u] = w2;
}
bool dfs(ll u, ll parent[], bool visit[])
{
visit[u] = true;
if (u == sink)
{
return true;
}
map<ll,ll>::iterator it;
ll ans = false;
for(it = rG[u].begin(); it != rG[u].end(); it++)
{
ll k = it->F;
ll w = it->S;
if (not visit[k] and w)
{
parent[k] = u;
ans = ans or dfs(k,parent,visit);
}
}
return ans;
}
bool bfs(ll parent[])
{
bool visit[V+1];
memset(visit,false,sizeof(visit));
queue<ll> q;
q.push(source);
visit[source] = true;
while (q.size() > 0)
{
ll u = q.front();
q.pop();
map<ll,ll>::iterator it;
for (it = rG[u].begin(); it != rG[u].end(); it++)
{
ll k = it->F;
ll w = it->S;
if (visit[k] or w == 0)
{
continue;
}
parent[k] = u;
q.push(k);
visit[k] = true;
if (visit[sink])
{
return true;
}
}
}
return visit[sink];
}
bool result(ll choice, ll parent[])
{
if (choice == 1)
{
return bfs(parent);
}
if (choice == 2)
{
bool visit[V+1];
memset(visit,false,sizeof(visit));
return dfs(source,parent,visit);
}
}
ll fordFulkerson(ll choice = 2)
{
ll ans = 0;
ll parent[V+1];
while (result(choice,parent))
{
ll pathFlow = (1e6);
for (ll v = sink; v != source; v = parent[v])
{
pathFlow = min(pathFlow,rG[parent[v]][v]);
}
for (ll v = sink; v != source; v = parent[v])
{
rG[parent[v]][v] -= pathFlow;
rG[v][parent[v]] += pathFlow;
}
ans += pathFlow;
}
return ans;
}
};