-
Notifications
You must be signed in to change notification settings - Fork 0
/
【模板】网络最大流Dinic.cpp
67 lines (67 loc) · 1.13 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
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1e4+5,maxm=1e5+5,inf=0x7ffffff;
int n,m,s,t,ans;
int head[maxn],num=1/*!!!!*/,cur[maxn],dep[maxn];
struct fy{int to,next,c;}q[maxm<<1];
void add(int a,int b,int c){
q[++num]=(fy){b,head[a],c};head[a]=num;
q[++num]=(fy){a,head[b],0};head[b]=num;
}
bool bfs()
{
memset(dep,0,sizeof dep);
queue<int>qq;
qq.push(s);
dep[s]=1;
while(!qq.empty())
{
int a=qq.front();qq.pop();
for(int i=head[a];i;i=q[i].next)
{
int b=q[i].to;
if(!dep[b]&&q[i].c)
{
dep[b]=dep[a]+1;
qq.push(b);
}
}
}
return dep[t];
}
int dfs(int a,int flow)
{
if(a==t||!flow) return flow;
int fl=0,f;
for(int &i=cur[a];i;i=q[i].next)
{
int b=q[i].to;
if(q[i].c&&dep[b]==dep[a]+1&&(f=dfs(b,min(flow,q[i].c))))
{
fl+=f;flow-=f;
q[i].c-=f;q[i^1].c+=f;
if(!flow) break;
}
}
return fl;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
while(bfs())
{
for(int i=1;i<=n;i++) cur[i]=head[i];
ans+=dfs(s,inf);
}
printf("%d\n",ans);
return 0;
}