-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdinic.cpp
80 lines (72 loc) · 1.74 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
# include <iostream>
# include <cstdio>
# include <cstring>
# include <queue>
using namespace std;
#define MAXN 10000
#define MAXE 100000
#define inf 0x3fffffff
struct Dinic{
struct node {
int x;
int f;
int next;
};
int h[MAXN], d[MAXN];
node e[MAXE];
int S, T, cnt;
void add_edge(int x,int y,int f) {
e[cnt].x = y; e[cnt].f = f;
e[cnt].next = h[x]; h[x] = cnt++;
e[cnt].x = x; e[cnt].f = 0;
e[cnt].next = h[y]; h[y] = cnt++;
}
void init() {
memset(h, 0, sizeof(h));
cnt = 2;
}
bool build() {
memset(d, -1, sizeof(d));
queue<int> q; q.push(S);
d[S] = 0;
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i=h[x]; i; i=e[i].next) {
if(e[i].f && d[e[i].x] == -1) {
d[e[i].x] = d[x] + 1;
q.push(e[i].x);
}
}
}
return d[T]!=-1;
}
int find(int x,int low = inf) {
if(x == T) return low;
int w = 0, ret = 0;
for(int i=h[x]; i&&w<low; i=e[i].next)
if(e[i].f && d[e[i].x]==d[x]+1) {
ret = find(e[i].x, min(e[i].f, low-w));
e[i].f -= ret; e[i^1].f += ret; w += ret;
}
if(!w) d[x] = -1;
return w;
}
int main(int s,int t) {
S = s; T = t;
int ans = 0;
while(build())
ans += find(S, inf);
return ans;
}
}D;
int main() {
int u, v, w, m, n;
while(~scanf("%d%d",&m,&n)) {
D.init();
for(int i=0; i<m; ++i) {
scanf("%d%d%d",&u,&v,&w);
D.add_edge(u,v,w);
}
printf("%d\n",D.main(1,n));
}
}