//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. MSSQL事务在C#程序端的使用

    拼接成一条SQL执行 优点:简单,容易看懂: 缺点:某些场合,涉及的业务较多,在同一SQL处理显得太冗长,复杂,不利于解耦. 使用细节 在方法之间传递参数,确保多个方法中的SQL都是使用同一个事务的( ...

  2. 【BZOJ 2241 打地鼠】

    Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1430  Solved: 908[Submit][Status][Discuss] Descripti ...

  3. ng4转义html

    https://stackoverflow.com/questions/31548311/angular-html-binding <div [innerHTML]="content& ...

  4. Optimize Prime Sieve

    /// A heavily optimized sieve #include <cstdio> #include <cstring> #include <algorith ...

  5. PHP正则匹配与替换的简单例子

    PHP正则匹配与替换的简单例子,含一个匹配获取加租字体例子和一个匹配替换超链接的例子. 1.查找匹配 <b> 与 </b> 标签的内容: <?php $str = &qu ...

  6. 动态规划:状压DP

    状压DP可以用在NP问题的小规模求解中(不理解,感觉和可以搜索的题很类似) 如果状态是个网格,数据范围很小,基本锁定状压DP 例题是BZOJ1725 题意是这样的,给定一个黑白图,然后种田,要求田与田 ...

  7. UVa11988 Broken Keyboard (a.k.a. Beiju Text)

    题目复制太麻烦了,甩个链接 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18693 直接模拟光标操作时间复杂度较高,所以用链 ...

  8. Codeforces Round #300 解题报告

    呜呜周日的时候手感一直很好 代码一般都是一遍过编译一遍过样例 做CF的时候前三题也都是一遍过Pretest没想着去检查... 期间姐姐提醒说有Announcement也自信不去看 呜呜然后就FST了 ...

  9. bzoj 1002 找规律(基尔霍夫矩阵)

    网上说的是什么基尔霍夫矩阵,没学过这个,打个表找下规律,发现 w[i]=3*w[i-1]-w[i-2]+2; 然后写个高精直接递推就行了 //By BLADEVIL var n :longint; a ...

  10. poj 2312 Battle City(优先队列+bfs)

    题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...