-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFord-Funkerson.cpp
More file actions
54 lines (53 loc) · 1.1 KB
/
Ford-Funkerson.cpp
File metadata and controls
54 lines (53 loc) · 1.1 KB
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
const int INF=1e9;
struct Edge
{
int from,to,cost,rev;
};
class FlowNetwork
{
public:
FlowNetwork(int n):graph(vector<vector<Edge>>(n)),used(vector<char>(n))
{}
void add_Edge(int from, int to, int cap);
int ford_fulkerson(int from, int to);
private:
vector<vector<Edge>> graph;
vector<char> used;
int dfs(int from, int to, int cap);
};
void FlowNetwork::add_Edge(int from, int to, int cap)
{
graph[from].push_back((Edge){from,to,cap,(int)graph[to].size()});
graph[to].push_back((Edge){to,from,0,(int)graph[from].size()-1});
}
int FlowNetwork::dfs(int from, int to, int f)
{
if(from == to) return f;
int d;
used[from]=true;
for(int i=0;i<graph[from].size();i++)
{
Edge &e=graph[from][i];
if(used[e.to] || e.cost<=0) continue;
d = dfs(e.to, to, min(e.cost, f));
if(d>0)
{
e.cost -= d;
graph[e.to][e.rev].cost += d;
return d;
}
}
return 0;
}
int FlowNetwork::ford_fulkerson(int from, int to)
{
int flow=0;
while(1)
{
fill(used.begin(),used.end(),0);
int f=dfs(from,to,INF);
if(f==0)break;
flow += f;
}
return flow;
}