-
Notifications
You must be signed in to change notification settings - Fork 439
/
Dinic.cpp
99 lines (89 loc) · 1.56 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 2000000000
#define VERTEX_COUNT 100000
#define EDGE_COUNT 100000
using namespace std;
struct vertex
{
int first, dis;
}V[VERTEX_COUNT];
struct edge
{
int endp, next, f;
}E[EDGE_COUNT];
int ivc, iec, ec = 2, src, sink;
inline void add_edge(int u, int v, int f)
{
E[ec].next = V[u].first;
V[u].first = ec;
E[ec].endp = v;
E[ec].f = f;
ec++;
}
bool bfs()
{
queue<int> Q;
static bool inq[VERTEX_COUNT];
memset(inq, false, sizeof(inq));
Q.push(sink), inq[sink] = true;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int cur = V[u].first; cur != 0; cur = E[cur].next)
{
if (E[cur ^ 1].f > 0 && inq[E[cur].endp] == false)
{
V[E[cur].endp].dis = V[u].dis + 1;
Q.push(E[cur].endp), inq[E[cur].endp] = true;
}
}
}
return inq[src] == true;
}
int dfs(int u, int curf)
{
if (u == sink) return curf;
int totalf = 0;
for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next)
{
if (V[u].dis == V[E[cur].endp].dis + 1 && E[cur].f > 0)
{
int f = dfs(E[cur].endp, min(E[cur].f, curf - totalf));
E[cur].f -= f;
E[cur ^ 1].f += f;
totalf += f;
}
}
return totalf;
}
int max_flow()
{
int res = 0;
while (bfs() == true)
{
int flow;
do
{
flow = dfs(src, INF);
res += flow;
} while (flow > 0);
}
return res;
}
int main()
{
int u, v, f;
scanf("%d%d", &iec, &ivc);
for (int i = 0; i < iec; i++)
{
scanf("%d%d%d", &u, &v, &f);
add_edge(u, v, f);
add_edge(v, u, 0);
}
src = 1, sink = ivc;
printf("%d", max_flow());
return 0;
}