最大流模板(洛谷3376)
来源:互联网 发布:黑米抢购软件靠谱吗 编辑:程序博客网 时间:2024/05/17 05:00
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#define rep(i,a,b) for(register int i=a;i<=b;i++)#define dep(i,a,b) for(register int i=a;i>=b;i--)#define reg(x) for(int i=last[x];i;i=e[i].next)#define ll long long#define mem(x,num) memset(x,num,sizeof(x))#define inf 1844387848using namespace std;inline ll read(){ ll f=1,x=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}const int maxn=1e5+6,maxq=1e6+6;struct edge{ll to,from,v,t,next;}e[maxn<<1];ll n,m,k,cnt=1,ans,s,t,last[maxn],que[maxq],h[maxq];bool bfs(){ ll head=0,tail=0,now; mem(h,-1);que[0]=s,h[s]=0; while(head<=tail){ now=que[head++]; reg(now) if(e[i].v&&h[e[i].to]==-1){h[e[i].to]=h[now]+1;que[++tail]=e[i].to;} } if(h[t]==-1)return 0;else return 1;}int dfs(int x,int f){ if(x==t)return f; ll w,used=0; reg(x) if(e[i].v&&h[e[i].to]==h[x]+1){ w=f-used;w=dfs(e[i].to,min(w,e[i].v)); e[i].v-=w;e[i^1].v+=w; used+=w;if(used==f)return f; } if(!used)h[x]=-1; return used;}void dinic(){while(bfs())ans+=dfs(s,inf);}int main(){ n=read(),m=read(),s=read(),t=read(); rep(i,1,m){ ll u=read(),v=read(),w=read(); e[++cnt]=(edge){v,u,w,w,last[u]};last[u]=cnt; e[++cnt]=(edge){u,v,0,-w,last[v]};last[v]=cnt; } dinic(); cout<<ans<<endl; return 0;}
阅读全文
0 0
- 最大流模板(洛谷3376)
- [洛谷]最大流 模板
- 最大流(模板)
- 最大流(模板)
- 洛谷 3376_[模板]网络最大流
- 【洛谷P3376】最大流模板(dinic)
- (洛谷 最小费用最大流 模板)
- 【模板】ISAP网络最大流 (模板题:洛谷P3376)
- 最大流模板(poj3469)
- 最大流模板(Dinic)
- p1273最大流(模板)
- poj3281最大流(模板)
- Luogu-3376 (网最大流模板-dinic)
- 洛谷 P3376【模板】网络最大流
- 【洛谷】3381 【模板】最小费用最大流
- 洛谷P3376【模板】网络最大流
- 洛谷 P3376 【模板】网络最大流
- 洛谷P3381【模板】最小费用最大流
- 序列化和反序列化
- ORACLE 12C下创建表空间和新用户
- C++模板(template)使用介绍
- 《Python学习手册》学习笔记(32)之第32章异常基础(关键词:编程语言/Python/异常)
- 企业管理软件
- 最大流模板(洛谷3376)
- Android Studio NDK项目编译备忘录
- 欢迎使用CSDN-markdown编辑器
- Linux下搭建JDK+Tomca环境
- 解析excel表获取数据,并从远程服务器下载和上传文件
- 【精品文章推荐及点评】六个人如何运维一万台服务器?
- python 面向对象(进阶篇)
- 微信官方小店小程序老徐解析
- Git For Windows