模板-网络流-Dinic
//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的更多相关文章
- 模板——网络流Dinic
感谢这位大佬的博客:https://www.cnblogs.com/SYCstudio/p/7260613.html 给予了我莫大的帮助! 主要说一下网络流的几个注意点: 1.和二分图匹配相似,无法继 ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- POJ 3281 [网络流dinic算法模板]
题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...
- 网络流dinic ek模板 poj1273
这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...
- Power Network POJ - 1459 网络流 DInic 模板
#include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...
- 网络流Dinic模板
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
随机推荐
- [HDU5956]The Elder
题面在这里 题意 一个王国中的所有城市构成了一棵有根树,其根节点为首都,编号为1 树有边权,城市的记者每次向祖先移动\(d\)的路程需要的代价为\(d^2\), 如果祖先不是根还需要加上\(p\),求 ...
- [NOI2017 D1T1]整数
题目大意:有一个整数 $x$ ,一开始为 $0$ .有 $n$ 个操作,有两种类型: $1 \;a\; b$:将 $x$ 加上整数 $a\cdot 2^b$ ,其中 $a$ 为一个整数, $b$ 为一 ...
- 2018牛客多校第一场 B.Symmetric Matrix
题意: 构造一个n*n的矩阵,使得Ai,i = 0,Ai,j = Aj,i,Ai,1+Ai,2+...+Ai,n = 2.求种类数. 题解: 把构造的矩阵当成邻接矩阵考虑. 那么所有点的度数都为2,且 ...
- MySQL触发器写法
触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/dele ...
- 使用命令wsimport生成WebService客户端
使用命令wsimport生成WebService客户端 wsimpost命令有几个重要的参数: -keep:是否生成java源文件 -d:指定输出目录 -s:指定源代码输出目录 -p ...
- Python之日志操作(logging)
import logging 1.自定义日志级别,日志格式,输出位置 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s | ...
- js实现日历
有这样一个普通的日历需求 第一反应就是找插件,结果找到了,但是改起来非常麻烦,然后查下实现的原理,发现原来很简单,于是自己实现了一个. 首先分析一下这个组件,每页显示的是 当前月的所有日期及所占据的行 ...
- Fragment使用--文章集锦
android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态 Android Fragment 真正的完全解析(上) Android Fragment实 ...
- input输入浮动提示
html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- BootStrap弹出框插件popover简单实例
1.网上实例地址 http://www.runoob.com/bootstrap/bootstrap-popover-plugin.html 2.具体demo $("#pieId&q ...