-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDinic_MaxFlow.cpp
More file actions
96 lines (87 loc) · 2.33 KB
/
Dinic_MaxFlow.cpp
File metadata and controls
96 lines (87 loc) · 2.33 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
template<typename flow_t = long long>
struct Dinic {
struct FlowEdge
{
int v, u;
flow_t cap, flow = 0;
FlowEdge(int v, int u, flow_t cap) : v(v), u(u), cap(cap) {}
};
const flow_t flow_inf = numeric_limits<flow_t>::max()/2;
vector<FlowEdge> edges;
vector<vector<int>> adj;
int n, m = 0;
int s, t;
// s -> source
// t -> sink
vector<int> level, ptr;
queue<int> q;
Dinic(int n, int s, int t) : n(n), s(s), t(t)
{
adj.resize(n+1);
level.resize(n+1);
ptr.resize(n+1);
}
void add_edge(int v, int u, flow_t cap)
{
edges.emplace_back(v, u, cap);
edges.emplace_back(u, v, 0);
adj[v].push_back(m);
adj[u].push_back(m + 1);
m += 2;
}
bool bfs()
{
while (!q.empty()) {
int v = q.front();
q.pop();
for (int id : adj[v]) {
if (edges[id].cap - edges[id].flow < 1)
continue;
if (level[edges[id].u] != -1)
continue;
level[edges[id].u] = level[v] + 1;
q.push(edges[id].u);
}
}
return level[t] != -1;
}
flow_t dfs(int v, flow_t pushed)
{
if (pushed == 0)
return 0;
if (v == t)
return pushed;
for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
int id = adj[v][cid];
int u = edges[id].u;
if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
continue;
flow_t tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
if (tr == 0)
continue;
edges[id].flow += tr;
edges[id ^ 1].flow -= tr;
return tr;
}
return 0;
}
flow_t flow()
{
flow_t f = 0;
while (true) {
fill(level.begin(), level.end(), -1);
level[s] = 0;
q.push(s);
if (!bfs())
break;
fill(ptr.begin(), ptr.end(), 0);
while (flow_t pushed = dfs(s, flow_inf)) {
f += pushed;
}
}
return f;
}
};
// .add_edge(u,v,cap);u->v
// .flow() == maxFlow
//Use - https://www.codechef.com/viewsolution/47741478