最大流
题目传送门
最大流模板题,EK算法居然过了!果然是远小于(不知道为什么题解里的那个人说不会过)。
(还不会最大流的童鞋们请戳这里)
注意第0号边也是有值的,不然1^1的时候就gg了。
废话不多说,上代码:
复制代码
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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct edge{
int next;
int to;
int flow;
int v;
};
struct fa{
int x;
int e;
};
edge a[200005];
fa father[10005];
int h[10005],r,w,k,n,m,s,e,u,v,d;
int remain[10005];
int b[10005];
bool f[10005];
void read(int x,int y,int z){
a[k].next=h[x];
a[k].to=y;
a[k].v=z;
h[x]=k;
k++;
a[k].next=h[y];
a[k].to=x;
a[k].v=0;
h[y]=k;
k++;
}
int bfs(){
memset(f,false,sizeof(f));
r=0; w=1;
b[w]=s;
f[s]=true;
remain[s]=0x7fffffff;
while (r<w){
int x=b[++r];
for (int i=h[x];~i;i=a[i].next){
if (!f[a[i].to]&&a[i].v-a[i].flow>0){
w++;
b[w]=a[i].to;
f[a[i].to]=true;
father[a[i].to].x=x;
father[a[i].to].e=i;
remain[a[i].to]=min(remain[x],a[i].v-a[i].flow);
if (a[i].to==e) return remain[e];
}
}
}
return 0;
}
void change(int x){
int now=e;
while (now!=s){
int ed=father[now].e;
a[ed].flow+=x;
a[ed^1].flow-=x;
now=father[now].x;
}
}
int maxflow(){
int ans=0;
while (1){
int sum;
sum=bfs();
if (!sum)
break;
ans+=sum;
change(sum);
}
return ans;
}
int main(){
scanf("%d%d%d%d",&n,&m,&s,&e);
memset(h,-1,sizeof(h));
for (int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&d);
read(u,v,d);
}
printf("%dn",maxflow());
return 0;
}
最后
以上就是清爽机器猫最近收集整理的关于洛谷P3376【模板】网络最大流最大流的全部内容,更多相关洛谷P3376【模板】网络最大流最大流内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复