-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFord_Fulkerson.cpp
More file actions
109 lines (90 loc) · 2.41 KB
/
Ford_Fulkerson.cpp
File metadata and controls
109 lines (90 loc) · 2.41 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
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
// ford-fulkerson: obter qual o fluxo maximo de um vertice s ate um vertice d
// 1 - rodar um bfs para descobrir um novo caminho de s ate d
// 2 - apos isso pego a aresta de menor custo desse caminho e subtraio o valor dela nas outras arestas do caminho
// 3 - fluxo_maximo += custo da aresta de menor custo desse caminho
// 4 - rodar isso ate nao existirem mais caminhos disponiveis (com fluxo diferente de 0) entre s e d
// 5 - o fluxo maximo de s ate d sera a soma das arestas de menor custo de cada caminho feito
#include <bits/stdc++.h>
using namespace std ;
#define lli long long int
#define pb push_back
#define MAXN 10000
#define INF 999999
int n , m , a , b , c , s , d , max_flow , flow ;
vector <int> parent ;
vector <int> adj [MAXN] ;
int cost [MAXN][MAXN] ;
bool visited [MAXN] ;
void get_menor_custo (int v , int mincost)
{
if (v == s)
{
flow = mincost ;
return ;
}
else if (parent[v] != -1)
{
get_menor_custo(parent[v] , min(mincost , cost[parent[v]][v])) ;
cost[parent[v]][v] -= flow ;
cost[v][parent[v]] += flow ;
}
}
void bfs ()
{
visited[s] = true ;
queue <int> q ;
q.push(s) ;
parent.assign(MAXN , -1) ;
while (!q.empty())
{
int u = q.front() ;
q.pop() ;
if (u == d)
{
break ;
}
for (int j = 0 ; j < adj[u].size() ; j++)
{
int v = adj[u][j] ;
if (cost[u][v] > 0 && !visited[v])
{
visited[v] = true ;
q.push(v) ;
parent[v] = u ;
}
}
}
}
int ford_fulkerson ()
{
max_flow = 0 ;
while (1)
{
flow = 0 ;
memset(visited , false , sizeof(visited));
bfs() ;
get_menor_custo(d , INF) ;
if (flow == 0)
{
break ;
}
max_flow += flow ;
}
return max_flow ;
}
int main ()
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
cin >> n >> m ;
for (int i = 0 ; i < m ; i++)
{
cin >> a >> b >> c ;
adj[a].pb(b);
adj[b].pb(a);
cost[a][b] = c ;
}
cin >> s >> d ;
cout << ford_fulkerson() << endl ;
return 0 ;
}