-
Notifications
You must be signed in to change notification settings - Fork 0
/
fordFulkerson1.cpp
82 lines (76 loc) · 1.13 KB
/
fordFulkerson1.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
struct MaxFlow
{
ll V, source, sink;
vector<vl> rG, G;
MaxFlow(ll v)
{
V = v+2;
source = v+1;
sink = v+2;
For(i,0,V+1)
{
vl t(V+1,0);
rG.pb(t);
}
For(i,0,V+1)
{
vl t;
G.pb(t);
}
}
void addEdge(ll u, ll v, ll w)
{
rG[u][v] = w;
rG[v][u] = 0;
G[u].pb(v);
G[v].pb(u);
}
bool dfs(ll u, ll parent[], bool visit[])
{
visit[u] = true;
if (u == sink)
{
return true;
}
ll ans = false;
For(i,0,(ll)G[u].size())
{
ll k = G[u][i];
if (visit[k])
{
continue;
}
if (rG[u][k] > 0)
{
parent[k] = u;
ans = ans or dfs(k, parent, visit);
}
}
return ans;
}
ll fordFulkerson()
{
ll ans = 0;
ll parent[V+1];
bool visit[V+1];
memset(visit,false,sizeof(visit));
while (dfs(source,parent,visit))
{
ll pathFlow = (1e15);
for (ll v = sink; v != source; v = parent[v])
{
ll u = parent[v];
pathFlow = min(pathFlow,rG[u][v]);
}
for (ll v = sink; v != source; v = parent[v])
{
ll u = parent[v];
rG[u][v] -= pathFlow;
rG[v][u] += pathFlow;
}
ans += pathFlow;
memset(visit,false,sizeof(visit));
}
return ans;
}
};