//Dinic
struct Edge{
int from,to,cap,flow;
Edge(){ }
Edge(int a,int b,int c,int d){
from=a;
to=b;
cap=c;
flow=d;
}
}edges[maxm*];
int n,m,s,t,sz;
vector<int> ve[maxn];
int dis[maxn],cur[maxn];
bool vis[maxn];
int l,r;
void addEdge(int a,int b,int c)
{
edges[sz++]=Edge(a,b,c,);
edges[sz++]=Edge(b,a,,);
ve[a].push_back(sz-);
ve[b].push_back(sz-);
}
bool BFS(){
memset(vis,,sizeof(vis));
queue<int> qu;
qu.push(s);
dis[s]=;
vis[s]=;
while(qu.size()){
int u=qu.front();qu.pop();
for(int i=;i<ve[u].size();i++){
Edge& e=edges[ve[u][i]];
if(!vis[e.to] && e.flow<e.cap){
vis[e.to]=;
qu.push(e.to);
dis[e.to]=dis[u]+;
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t || a==)return a;
int flow=,f;
for(int& i=cur[x];i<ve[x].size();i++){
Edge e=edges[ve[x][i]];
if(dis[x]+==dis[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>){
flow+=f;
a-=f;
edges[ve[x][i]].flow+=f;
edges[ve[x][i]^].flow-=f;
if(a==)break;
}
}
return flow;
}
int dinic()
{
int flow=;
while(BFS()){
memset(cur,,sizeof(cur));
flow += DFS(s,);
}
return flow;
}

模板-网络流-Dinic的更多相关文章

  1. 模板——网络流Dinic

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

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

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

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

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

  4. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  5. POJ 3281 [网络流dinic算法模板]

    题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...

  6. 网络流dinic ek模板 poj1273

    这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...

  7. Power Network POJ - 1459 网络流 DInic 模板

    #include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...

  8. 网络流Dinic模板

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...

  9. 网络流dinic模板,邻接矩阵+链式前向星

    //这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...

随机推荐

  1. [AHOI2013]作业 & Gty的二逼妹子序列 莫队

    ---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...

  2. GDI+小例子

    原文链接地址:http://www.cnblogs.com/chuanzifan/archive/2011/11/26/2264507.html 1.在stdafx.h中 #include <G ...

  3. mysql共享表空间和独立表空间

    innodb这种引擎,与MYISAM引擎的区别很大.特别是它的数据存储格式等. 对于innodb的数据结构,首先要解决两个概念性的问题: 共享表空间以及独占表空间. 什么是共享表空间和独占表空间 共享 ...

  4. Codeforces Round #535 (Div. 3) 题解

    Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...

  5. wyh的天鹅~vector的使用

    链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 262144K,其他语言52428 ...

  6. Linux下rsync 安装与配置

    1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...

  7. jQuery知识点:attr与prop的区别

    做项目时遇到个莫名的问题,全选的时候仅第一次有效,再次点击全选按钮是无效了,查了查原因,看到篇很不错的文章,问题出在jquery中的attr属性上,这里做下笔记. 原文链接:http://www.cn ...

  8. codevs 3305 水果姐逛水果街Ⅱ&&codevs3006

    题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔法,水果街变成了树结构(店与店之间只有一条唯一的路径). 同样还是n家水果店,编号为1~ ...

  9. Manipulating Files

    http://linuxcommand.org/lc3_lts0050.php This lesson will introduce you to the following commands: cp ...

  10. apparmor开启