博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【最大流】【CODEVS】1993 草地排水
阅读量:6652 次
发布时间:2019-06-25

本文共 1503 字,大约阅读时间需要 5 分钟。

【算法】网络流-最大流(dinic)

【题解】http://www.cnblogs.com/onioncyc/p/6496532.html

#include
#include
#include
using namespace std;const int maxn=210,inf=0x3f3f3f3f;struct edge{
int from,v,flow;}e[maxn*3];int n,m,tot=2,S,T,d[maxn],q[510],first[maxn],cur[maxn];void insert(int u,int v,int flow){tot++;e[tot].v=v;e[tot].flow=flow;e[tot].from=first[u];first[u]=tot;}bool bfs(){ memset(d,-1,sizeof(d)); int head=0,tail=1;q[head]=S; d[S]=0; while(head!=tail) { int x=q[head++];if(head>=501)head=0; for(int i=first[x];i;i=e[i].from) if(d[e[i].v]==-1&&e[i].flow>0) { q[tail++]=e[i].v;d[e[i].v]=d[x]+1; if(tail>=501)tail=0; } } if(d[T]==-1)return 0;else return 1;}int dfs(int x,int a){ if(x==T||a==0)return a; int flow=0,f; for(int& i=cur[x];i;i=e[i].from)//当前弧优化 if(d[e[i].v]==d[x]+1&&(f=dfs(e[i].v,min(a,e[i].flow)))>0) { e[i].flow-=f; e[i^1].flow+=f; flow+=f; a-=f; if(a==0)break; } return flow;}int main(){ scanf("%d%d",&m,&n); S=1;T=n; for(int i=1;i<=m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); insert(u,v,w); insert(v,u,0); } int ans=0; while(bfs()) { for(int i=1;i<=n;i++)cur[i]=first[i]; ans+=dfs(S,inf); } printf("%d",ans); return 0;}
View Code

 

转载于:https://www.cnblogs.com/onioncyc/p/6421652.html

你可能感兴趣的文章
python之文件操作、OS模块、CSV、 面向对象
查看>>
win10-1079版本无法匿名访问SMB共享文件夹
查看>>
AngularJs 基础教程 —— 与php服务器
查看>>
centos7 搭建SVN环境
查看>>
Cookie
查看>>
第一周总结
查看>>
一分钟完成MySQL5.7安装部署
查看>>
ORA-10458、ORA-01196和ORA-01110错误解决办法
查看>>
Python和Ruby哪个好学?python基础
查看>>
为什么喜欢自驾游?
查看>>
爱创课堂每日一题九十三天-html常见兼容性问题?
查看>>
2.10 环境变量PATH 2.11 cp命令 2.12 mv命令 2.13 文档查看cat/mor
查看>>
OSI模型
查看>>
Java之品优购课程讲义_day14(7)
查看>>
大快DKhadoop大数据处理平台详解
查看>>
如何制作出像顶尖咨询公司咨询顾问们那种风格的 PPT?
查看>>
共享经济影响电商 华亚优品商城凭“共创共享”模式受捧
查看>>
Swarm群集搭建
查看>>
shell
查看>>
造成HashMap非线程安全的原因
查看>>