洛谷 P3376【模板】网络最大流
来源:互联网 发布:德国男友知乎 编辑:程序博客网 时间:2024/06/05 08:24
题目描述
如题,给出一个网络图,以及其源点和汇点,求出其网络最大流。
【题目分析】
网络流模板题目 (Dinic)
【代码】
#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <set>#include <map>#include <string>#include <algorithm>#include <vector>#include <iostream>#include <queue>using namespace std;#define maxn 200005#define inf 1000000000int read(){ int x=0,f=1; 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;}int fr[maxn],to[maxn],ne[maxn],h[maxn],mp[maxn],f[maxn];int n,m,en,S,T;queue <int> q;void add(int a,int b,int r){ fr[en]=a;to[en]=b;ne[en]=h[a];f[en]=r;h[a]=en++; fr[en]=b;to[en]=a;ne[en]=h[b];f[en]=0;h[b]=en++;}bool tell(){ memset(mp,-1,sizeof mp); q.push(S); mp[S]=0; while (!q.empty()) { int x=q.front();q.pop(); for (int i=h[x];i>=0;i=ne[i]) if (mp[to[i]]==-1&&f[i]) { mp[to[i]]=mp[x]+1; q.push(to[i]); } } if (mp[T]==-1) return false; return true;}int zeng(int k,int now){ if (k==T) return now; int ret=0; for (int i=h[k];i>=0&&now>ret;i=ne[i]) if (mp[k]+1==mp[to[i]]&&f[i]!=0) { int tmp=zeng(to[i],min(now-ret,f[i])); f[i]-=tmp; f[i^1]+=tmp; ret+=tmp; } if (!ret) mp[k]=-1; return ret;}int main(){ memset(h,-1,sizeof h); n=read();m=read();S=read();T=read(); for (int i=1;i<=m;++i) { int a=read(),b=read(),c=read(); add(a,b,c); } int ans=0,tmp; while (tell()) while (tmp=zeng(S,inf)) ans+=tmp; printf("%d\n",ans);}
0 0
- 洛谷 P3376【模板】网络最大流
- 洛谷P3376【模板】网络最大流
- 洛谷 P3376 【模板】网络最大流
- P3376 【模板】网络最大流
- 【模板】ISAP网络最大流 (模板题:洛谷P3376)
- Dinic算法 P3376 【模板】网络最大流
- P3376 网络最大流
- 【洛谷P3376】最大流模板(dinic)
- 网络流最大流入门(洛谷P3376)
- 【Dinic模板】【洛谷P3376】
- 最大网络流 模板
- 【模板】网络最大流
- 洛谷 3376_[模板]网络最大流
- poj 网络最大流模板
- [模板练习]网络最大流
- 网络流最大流模板(poj1273)
- 网络流最大流EdmondKarp、SAP【模板】
- 网络流最大流sap算法模板
- DB2执行计划
- FragementTabHost简单用法
- snowflake 算法
- docker容器内pip更新国内镜像源
- 利用SQLCipher加解密数据库(包括加解密已有的数据库)
- 洛谷 P3376【模板】网络最大流
- 启动docker容器提示"docker: Error response from daemon: Container command not found or does not exist"的原因
- 24 反射learning reflect
- 第15篇 安卓一对一测试问题(三)更新代码(一)
- ==和equals的区别
- 最好的验证码识别软件api接口
- File类
- SSL P1612 最优布线问题
- UE4中自定义的碰撞几何体使用注意事项