bool bfs(){
memset(deep,0,sizeof(deep));
queue<int>que;
que.push(s);
deep[s]=1;
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=head[u];i!=-1;i=e[i].nextt){
int v=e[i].v;
if(e[i].w>0&&deep[v]==0){
deep[v]=deep[u]+1;
if(v==t)
return true;
que.push(v);
}
}
}
return deep[t]==0?false:true;
}
int dfs(int u,int fl){
if(u==t)
return fl;
int ans=0,x=0;
for(int i=cur[u];i!=-1;i=e[i].nextt){
int v=e[i].v;
if(e[i].w>0&&deep[v]==deep[u]+1){
x=dfs(v,min(e[i].w,fl-ans));
e[i].w-=x;
e[i^1].w+=x;
if(e[i].w)
cur[u]=i;
ans+=x;
if(ans==fl)
return ans;
}
}
if(ans==0)
deep[u]=0;
return ans;
}
int dinic(int n){
int ans=0;
while(bfs()){
for(int i=0;i<=n;i++)
cur[i]=head[i];
ans+=dfs(s,inf);
}
return ans;
}

  

网络流dinic板子的更多相关文章

  1. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  2. 最大网络流dinic

    初始化flow(最大流量)为INF(极大值),建边(正向弧和反向弧) bfs寻找增广路看看有没有路,顺便进行深度标号.如果没有路直接结束输出flow. 如果有,我们按照深度dfs.dfs时注意在给正向 ...

  3. ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)

    //有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...

  4. 网络流Dinic(本篇介绍最大流)

    前言:看到网上Dinic和ISAP的比较,多数人认为ISAP更快,不容易爆栈.当然,也有少数人认为,在多数情况下,Dinic比较稳定.我认为Dinic的思路比ISAP更简明,所以选择了Dinc算法 介 ...

  5. Drainage Ditches(POJ1273+网络流+Dinic+EK)

    题目链接:poj.org/problem?id=1273 题目: 题意:求最大流. 思路:测板子题,分别用Dinic和EK实现(我的板子跑得时间均为0ms). Dinic代码实现如下: #includ ...

  6. 网络流Dinic算法模板 POJ1273

    这就是以后我的板子啦~~~ #include <queue> #include <cstdio> #include <cstring> #include <a ...

  7. POJ 2987 Firing 最大流 网络流 dinic 模板

    https://www.cnblogs.com/137shoebills/p/9100790.html http://poj.org/problem?id=2987 之前写过这道题,码一个dinic的 ...

  8. 模板——网络流Dinic

    感谢这位大佬的博客:https://www.cnblogs.com/SYCstudio/p/7260613.html 给予了我莫大的帮助! 主要说一下网络流的几个注意点: 1.和二分图匹配相似,无法继 ...

  9. 图论-网络流-Dinic (邻接表版)

    //RQ的板子真的很好用 #include<cstdio> #include<cstring> #include<queue> #define INF 1e9 us ...

随机推荐

  1. XML--XML Schema Definition(三)

    参考 http://www.w3school.com.cn/schema/index.asp XSD 复合元素 复合元素指包含其他元素及/或属性的 XML 元素. 有四种类型的复合元素: 空元素 包含 ...

  2. Maven:Failure executing javac, but could not parse the error:javac: 无效的目标发行版: 1.8

    eclipse中对着项目maven——>>maven install时出现错误:Failure executing javac, but could not parse the error ...

  3. wordcloud安装错误信息的解决

    参考链接:https://www.zhihu.com/people/si-kong-ji-54/posts 在windows下command里运行指令   pip install wordcloud  ...

  4. Jshint 安装方法

    首先在编辑器中搜索扩展程序 "Jshint" 并安装,安装成功后 打开Javascript文件会出现报错提示: "Failed to load jshint librar ...

  5. Java 使用控制台操作实现数据库的增删改查

    使用控制台进行数据库增删改查操作,首先创建一个Java Bean类,实现基础数据的构造,Get,Set方法的实现,减少代码重复性. 基本属性为 学生学号 Id, 学生姓名 Name,学生性别 Sex, ...

  6. Vue.js(15)之 json-server搭建模拟的API服务器

    json-server搭建模拟的API服务器 运行命令 npm install json-server -D 全局安装 json-server 项目根目录下创建 mock 文件夹 mock 文件夹下添 ...

  7. UVALive 6491 You win! 状态DP

    这个题目上周的对抗赛的,美国2013区域赛的题目,上次比赛真惨,就做出一道题,最多的也只做出两道,当时想把这题做出来,一直TLE. 这个题目用挂在Hunnu OJ的数据可以过,但UVALive上死活过 ...

  8. centos系统将shell脚本改成systemctl启动的形式

    说明: CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,就将程序存在系统服务里,即 ...

  9. java笔记之输入输出流

    做统计单词个数时的笔记

  10. Python说文解字_继承过程中的参数集合

    1. 先看一段属性继承的代码: class User: def __init__(self,name,age): self.name = name self.age = age class User1 ...