-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdinic.cpp
More file actions
85 lines (74 loc) · 1.46 KB
/
Copy pathdinic.cpp
File metadata and controls
85 lines (74 loc) · 1.46 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
#include<bits/stdc++.h>
using namespace std;
const int N=10010,M=200010,INF=1e8;
int n,m,s,t;
int he[N],no[M],fl[M],ne[M],idx;
int q[N],lev[N],notfull[N];
void add(int a, int b, int c){
no[idx]=b,fl[idx]=c,ne[idx]=he[a],he[a]=idx++;
no[idx]=a,fl[idx]=0,ne[idx]=he[b],he[b]=idx++;
}
bool bfs(){
int qh=0,qt=0;
memset(lev,-1,sizeof lev);
q[0]=s,lev[s]=0,notfull[s]=he[s];
while(qh<=qt){
int top=q[qh++];
for(int i=he[top];~i;i=ne[i]){
int j=no[i];
if(lev[j]==-1&&fl[i]){
lev[j]=lev[top]+1;
notfull[j]=he[j];
if(j==t)return true;
q[++qt]=j;
}
}
}
return false;
}
int dfs(int u,int limit){
if(u==t)return limit;
int flow=0;
for(int i=notfull[u];~i&&flow<limit;i=ne[i]){
notfull[u]=i;
int j=no[i];
if(lev[j]==lev[u]+1&&fl[i]){
int top=dfs(j,min(fl[i],limit-flow));
if(!top)lev[j]=-1;
fl[i]-= top,fl[i^1]+=top,flow+=top;
}
}
return flow;
}
int dinic(){
int res=0,flow;
while(bfs()){
while(flow=dfs(s,INF)){
res+=flow;
}
}
return res;
}
void ini(){
memset(he,-1,sizeof he);
}
int getid(int i,int isleft){
return 2*i-isleft;
}
void backtrack(){
for(int i=0;i<idx;i+=2){
fl[i]+=fl[i^1];
fl[i^1]=0;
}
}
int main(){
scanf("%d%d%d%d",&n,&m,&s,&t);
ini();
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
printf("%d",dinic());
return 0;
}