目录

ford fulkerson algorithm

目录
 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
//O(FE) 
/* 
input 
5 7
1 3 6
3 5 8
2 5 5
3 2 3
1 2 6
4 1 10
4 2 2
4 5
output
11
*/
#include<bits/stdc++.h>
using namespace std;
#define maxv 100
#define inf 0x3f3f3f3f
int v,e;
int s,t;
struct edge{int to,cap,rev;
};
vector<edge> G[maxv];
bool used[maxv];

void add_edge(int from ,int to,int cap)
{
	G[from].push_back((edge){to,cap,G[to].size()});
	G[to].push_back((edge){from,0,G[from].size()-1});
}

int dfs(int s,int t,int f)
{
	if(s==t) return f;
	used[s]=true;
	for(int i=0;i<G[s].size();i++)
	{
		edge &e =G[s][i];
		if(!used[e.to] && e.cap>0)
		{
			int d=dfs(e.to,t,min(f,e.cap));
			if(d>0)
			{
				e.cap-=d;
				G[e.to][e.rev].cap+=d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(int s,int t)
{
	int flow=0;
	for(;;)
	{
		memset(used,0,sizeof(used));
		int f=dfs(s,t,inf);
		if(f==0) return flow;
		flow+=f;
	}
}

int main()
{
	freopen("input.txt","r",stdin);
	cin>>v>>e;
	for(int i=0;i<e;i++)
	{
		int from,to,cap;
		scanf("%d%d%d",&from,&to,&cap);
		add_edge(from,to,cap);
	}
	cin>>s>>t;
	cout<<max_flow(s,t);
	return 0;
}