-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDinic.cpp
82 lines (69 loc) · 1.66 KB
/
Dinic.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
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const int MAX_V = 400;
struct edge{
int u, v, cap, flow;
};
int V, s, t, ptr[MAX_V], dist[MAX_V];
vector<edge> e;
vector<int> g[MAX_V];
void addEdge(int u, int v, int cap){
edge e1 = {u, v, cap, 0};
edge e2 = {v, u, 0, 0};
g[u].push_back((int)e.size());
e.push_back(e1);
g[v].push_back((int)e.size());
e.push_back(e2);
}
bool bfs(){
memset(dist, -1, sizeof(dist));
queue<int> q;
q.push(s);
dist[s] = 0;
while(!q.empty() && dist[t] == -1){
int u = q.front(); q.pop();
for (int id : g[u]){
int to = e[id].v;
if (dist[to] == -1 && e[id].flow < e[id].cap){
q.push(to);
dist[to] = dist[u] + 1;
}
}
}
return dist[t] != -1;
}
int dfs(int v, int flow){
if (!flow) return 0;
if (v == t) return flow;
for(; ptr[v] < g[v].size(); ptr[v]++){
int id = g[v][ptr[v]], to = e[id].v;
if (dist[to] != dist[v] + 1) continue;
int pushed = dfs(to, min(flow, e[id].cap - e[id].flow));
if (pushed){
e[id].flow += pushed;
e[id ^ 1].flow -= pushed;
return pushed;
}
}
return 0;
}
int dinic(){
int mf = 0;
while(true){
if (!bfs()) break;
memset(ptr, 0, sizeof(ptr));
while(int pushed = dfs(s, INF)) mf += pushed;
}
return mf;
}
int main()
{
int V, E; scanf("%d %d", &V, &E);
for (int i = 0; i < E; i++){
int u, v, w; scanf("%d %d %d", &u, &v, &w);
addEdge(u, v, w);
}
scanf("%d %d", &s, &t);
printf("%d\n", dinic());
}