模板-网络流-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. ...
随机推荐
- 【题解】HNOI2010合唱队
我果然还是太弱了呜呜呜……洛谷P3205 区间dp:注意到一段区间最两侧的人必然是最后加入队伍的所以由此我们可以分成两种情况来讨论. 一种是最后一个加入的人是左边的,另一种是右边的.那么状态:dp[i ...
- [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- final变量属性小记
final 修饰符对于类成员变量来说,具备语法上不可变的特性:对于类成员方法来说,具备语法上子类不可覆盖重写的特性(能被继承的前提下). 但 final 并不限制子类对父类被修饰声明的成员变量进行覆盖 ...
- Dubbo入门介绍---搭建一个最简单的Demo框架
Dubbo入门---搭建一个最简单的Demo框架 置顶 2017年04月17日 19:10:44 是Guava不是瓜娃 阅读数:320947 标签: dubbozookeeper 更多 个人分类: D ...
- angular js自定义service的简单示例
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Ubuntu1604 install netease-cloud music
Two issue: 1. There is no voice on my computer, and the system was mute and cannot unmute. eric@E641 ...
- tr/td
在HTML中,tr代表行,td代表列. 说明: 1.tr与td必须一起使用,并且输入的内容必须在td里面: 2.td必须在tr里面,表示在一行中的列: 3.在一个tr里面,有x个td,就表示在这一行里 ...
- WEB API 版本控制
参照 http://blog.csdn.net/hengyunabc/article/details/20506345
- Windows autoKeras的下载与安装连接
autoKeras autoKeras GitHub:https://github.com/jhfjhfj1/autokeras 百度网盘下载地址:http://pandownload.com/ 大牛 ...
- bzoj 2749 杂题
我们可以发现,phi(x)与x相比,相当于x的每个质因子-1后再分解质因数,添加到现有的质因子中,比如质因子13相当于将13变成12,然后分解成2*2*3,再将2的质数+2,3的指数+1,除了质因子2 ...