-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFordFulkerson.cpp
70 lines (64 loc) · 1.13 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
64
65
66
67
68
69
70
// Fast Ford fullkerson with scaling!
const int maxNode=10000;
VI cap[maxNode];
VI adjList[maxNode];
VI revmap[maxNode];
int deg[maxNode];
bool done[maxNode];
int scale;
void initFlow()
{
for(int i=0;i<maxNode;i++)
{
adjList[i].clear();
cap[i].clear();
revmap[i].clear();
deg[i]=0;
}
}
void addEdge(int u,int v,int cap1,int cap2)
{
revmap[u].pb(deg[v]);
revmap[v].pb(deg[u]);
adjList[u].pb(v);
adjList[v].pb(u);
cap[u].pb(cap1);
cap[v].pb(cap2);
deg[u]++;
deg[v]++;
}
int visit(int cur,int dest,int flow)
{
done[cur]=true;
if(cur==dest) return flow;
for(int k=0;k<deg[cur];k++)
{
int v = adjList[cur][k];
if(!done[v] && cap[cur][k]>=scale)
{
int z=visit(v,dest,min(flow,cap[cur][k]));
if(z>0)
{
cap[cur][k] -= z;
cap[v][revmap[cur][k]] += z;
return z;
}
}
}
return 0;
}
int flow (int src, int dest)
{
int total = 0;
scale = 1 << 25;
while(scale > 0)
{
fill(done,false);
int z = visit(src,dest,(int)1e9);
if(z==0)
scale /= 2;
else
total += z;
}
return total;
}