网络流Dinic算法
我的模板
例题: https://vjudge.net/problem/HDU-4280
struct Edge {
int lst;
int from;
int to;
int cap;
int flow;
Edge () { }
Edge (int llst, int ffrom, int tto, int ccap, int fflow) : lst(llst), from(from), to(tto), cap(ccap), flow(fflow) { }
};
// Dinic 算法有3个重点:
// 一个是 层次图
// 一个是 阻塞流
// 一个是 cur优化
// 稠密点的可以再加上 炸点优化
class Dinic {
public:
Edge edge[maxn*];
int head[maxn];
int cn, cm, cst, ced;
int csz;
bool vis[maxn];
int dist[maxn]; // 节点到起点层次距离
int cur[maxn];
void init(int n, int m) {
cn = n; cm = m;
memset(head, , sizeof(head));
csz = ; // 注意 这儿应该是偶数开始 因为 奇数^1等价于减1 偶数^1等价于加一.
// 因为我前向星是以0为结尾的 所以我必须从2开始.
}
void add(int u, int v, int c) {
edge[csz] = Edge(head[u], u, v, c, );
head[u] = csz++;
edge[csz] = Edge(head[v], v, u, , ); // 反向边,记得容量为0, 但是有些情况是可以修改的. 例如在建无向图的时候.
head[v] = csz++;
}
// bfs划 层次图
bool bfs() {
memset(vis, , sizeof(vis));
queue<int> q;
q.push(cst);
vis[cst] = true;
dist[cst] = ;
int i, v, u;
while (!q.empty()) {
u = q.front(); q.pop();
for (i=head[u]; i; i=edge[i].lst) {
v = edge[i].to;
if (!vis[v] && edge[i].cap > edge[i].flow) {
dist[v] = dist[u] + ;
vis[v] = true;
q.push(v);
}
}
}
return vis[ced]; // 说明不能到达汇点
}
// dfs收割阻塞流
int dfs(int u, int limit) {
if (u==ced || limit==) return limit;
int i, v, flow = , f;
for (i=cur[u]; i; i=cur[u]=edge[i].lst) {
v = edge[i].to;
if (dist[u]+==dist[v] && (f = dfs(v, min(limit, edge[i].cap - edge[i].flow))) > ) { // 找增广路
edge[i].flow += f;
edge[i^].flow -= f;
flow += f;
limit -= f;
if (limit == ) break;
}
}
if (!flow) dist[u] = -; // 这个第二个优化:炸点优化,说明这个点没有贡献了,再经过他也没有任何意义了,所以距离定义为-1.
return flow;
}
// 不断分割层次图,每一次分割后就收割一次阻塞流,直到不能再分割层次图
int maxflow(int st, int ed) {
cst = st; ced = ed;
int i, res = ;
while (bfs()) {
// 这是一个优化, cur[u]表示上次u节点访问到的dfs位置. 这样就不用每一次都从head[u]开始.
// 可以避免很多重复计算,如果没有这个优化,那么退化成Edmonds_Karp算法了... 这是第一个优化 还有一个炸点优化.
for (i=; i<=cn; ++i) cur[i] = head[i];
res += dfs(st, inf);
}
return res;
}
}Dic;
紫书模板
struct Edge {
int from, to, cap, flow;
Edge (int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) { }
};
struct Dinic {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n) {
this->n = n;
for (int i=; i<=n; ++i) G[i].clear();
edges.clear();
}
inline void add(int u, int v , int val) {
edges.push_back(Edge(u, v, val, ));
edges.push_back(Edge(v, u, , ));
m = edges.size();
G[u].push_back(m-);
G[v].push_back(m-);
}
bool BFS() {
memset(vis, , sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = ;
vis[s] = true;
while (!Q.empty()) {
int x = Q.front(); Q.pop();
for (int i=, sz=G[x].size(); i<sz; ++i) {
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x==t || a==) return a;
int flow = , f;
for (int &i=cur[x], sz = G[x].size(); i<sz; ++i) {
Edge &e = edges[G[x][i]];
if (d[x] + == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > ) {
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = ;
while (BFS()) {
memset(cur, , sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}
}Dic;
网络流Dinic算法的更多相关文章
- poj 1459 Power Network : 最大网络流 dinic算法实现
点击打开链接 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20903 Accepted: ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- 网络流(dinic算法)
网络最大流(dinic) 模型 在一张图中,给定一个源点s,给定汇点t,点之间有一些水管,每条水管有一个容量,经过此水管的水流最大不超过容量,问最大能有多少水从s流到t(s有无限多的水). 解法 di ...
- 高效的网络流dinic算法模版
#include <cstring> #include <algorithm> #include <vector> #define Maxn 120010 #def ...
- POJ 3281 [网络流dinic算法模板]
题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...
- 网络流Dinic算法模板 POJ1273
这就是以后我的板子啦~~~ #include <queue> #include <cstdio> #include <cstring> #include <a ...
- POJ 3281 网络流dinic算法
B - Dining Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- [知识点]网络流之Dinic算法
// 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html ...
- 网络流入门—用于最大流的Dinic算法
"网络流博大精深"-sideman语 一个基本的网络流问题 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3 ...
随机推荐
- Mysql SQL优化系列之——执行计划连接方式浅释
关系库SQL调优中,虽然思路都是一样的,具体方法和步骤也是大同小异,但细节却不容忽视,尤其是执行计划的具体细节的解读中,各关系库确实有区别,特别是mysql数据库,与其他关系库的差别更大些,下面,我们 ...
- RocketMQ消息存储
转载:RocketMQ源码学习--消息存储篇 消息中间件—RocketMQ消息存储(一) RocketMQ高性能之底层存储设计 存储架构 RMQ存储架构 上图即为RocketMQ的消息存储整体架构,R ...
- Mysql索引引起的死锁
提到索引,首先想到的是效率提高,查询速度提升,不知不觉都会有一种心理趋向,管它三七二十一,先上个索引提高一下效率..但是索引其实也是暗藏杀机的... 今天压测带优化项目,开着Jmeter高并发访问项目 ...
- 函数后面跟throw
1.函数后面跟throw(),表示该函数不会抛出异常 2.函数后面跟throw(...),表示该函数可能会抛出任何形式的异常 3.函数后面跟throw(int),表示该函数只抛出int类型的异常
- am335x system upgrade uboot sd boot(一)
由于上层应用的需求,需要运行arm docker,在kernel3.2上面还不支持,且编译器的glibc版本比较低的问题,故需要做系统升级 新的内核4.14.40驱动开发和以往有很大的不同,关键在于d ...
- JAVA的环境变量配置(方式二)
1.想要成功配置Java的环境变量,那肯定就要安装JDK(JDK安装包在方式一中),才能开始配置的. 2.安装JDK 向导进行相关参数设置.如图: 3.正在安装程序的相关功能,如图: 4.选择安装的路 ...
- 今天心情大好,在bluemix部署了一个hell-oworld。
虽然不是什么很NB的事情. 但是已经开始了. 基于kubernetes容器技术,在kubernetes集群中部署docker镜像hello-world,并正确映射到集群的80端口. 听着老TM复杂了. ...
- vue组件的使用和事件传递
子组件与父组件的事件传递具体实现如下: 子组件: <template> <section class="xftz-data-list"> <div c ...
- Cracking The Coding Interview5.1
//You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set a ...
- 无法获取 vmci 驱动程序版本: 句柄无效
https://jingyan.baidu.com/article/a3a3f811ea5d2a8da2eb8aa1.html 将 vmci0.present = "TURE" 改 ...