网络流 - dinic + 当前弧优化【代码】
- 这是初学网络流的时候从《算法竞赛进阶指南》抄下来的一份代码,自己理解的也不是很透彻。
- 注意,边要从 \(1\) 开始计,不然直接 \(xor\) 运算的话取反边会直接炸掉。
#include <bits/stdc++.h>
#define int long long
namespace Basic {
template <typename Temp> inline void read(Temp & res) {
Temp fh = 1; res = 0; char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') fh = -1;
for(; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ '0');
res = res * fh;
}
template <typename Temp> inline void Checkmax(Temp & res, Temp comp) {if(comp > res) res = comp;}
template <typename Temp> inline void Checkmin(Temp & res, Temp comp) {if(comp < res) res = comp;}
}
using namespace std;
using namespace Basic;
const int Maxn = 2e2 + 5;
const int Maxm = 5e3 + 5;
const int INF = 0x7fffffff >> 1;
struct e {
int to, nxt, flow;
} b[Maxm << 1];
int head[Maxn], ecnt = 1;
inline void add(int u, int v, int f) {b[++ecnt] = (e){v, head[u], f}; head[u] = ecnt;}
int depth[Maxn], curr[Maxn];
int n, m, st, ed;
int Max_flow, last_flow;
queue<int> q;
inline bool bfs() {
memset(depth, 0, sizeof(depth));
while(!q.empty()) q.pop();
depth[st] = 1; q.push(st); curr[st] = head[st];
while(!q.empty()) {
int tnow = q.front(); q.pop();
for(register int i = head[tnow]; i; i = b[i].nxt) {
int tto = b[i].to, tw = b[i].flow;
if((!tw) || (depth[tto])) continue;
q.push(tto);
depth[tto] = depth[tnow] + 1; curr[tto] = head[tto];
if(tto == ed) return true;
}
}
return false;
}
int dfs(int t, int Flow) {
if(t == ed) return Flow;
int rest = Flow, k, i;
for(i = curr[t]; i && rest; i = b[i].nxt) {
int tto = b[i].to, tw = b[i].flow; curr[t] = i;
if((tw == 0) || (depth[tto] != depth[t] + 1)) continue;
k = dfs(tto, min(rest, tw));
rest -= k;
b[i].flow -= k;
b[i ^ 1].flow += k;
if(!k) depth[tto] == 0;
}
return Flow - rest;
}
signed main() {
read(n); read(m); read(st); read(ed);
int x, y, z;
while(m--) {
read(x); read(y); read(z);
add(x, y, z);
add(y, x, 0);
}
while(bfs()) {
/*
for(register int i = 1; i <= n; ++i) {
printf("%d ", depth[i]);
}
puts("");
*/
while(last_flow = dfs(st, INF)) Max_flow += last_flow;
}
printf("%d", Max_flow);
return 0;
}
网络流 - dinic + 当前弧优化【代码】的更多相关文章
- ARC085E(最小割规划【最大流】,Dinic当前弧优化)
#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...
- [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]
题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...
- P3376 网络流-最大流模板题(Dinic+当前弧优化)
(点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处 ...
- Dinic当前弧优化 模板及教程
在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...
- HDU 4280 Island Transport(dinic+当前弧优化)
Island Transport Description In the vast waters far far away, there are many islands. People are liv ...
- 【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)
题目链接 妈耶 我的图建反了两次 准确的说是有两个地方建反了,然后反上加反改了一个小时…… 知道为什么要拆点吗? 假设这是你的图 左边到右边依次是超级源点 练习册 书 答案 ...
- 网络流小记(EK&dinic&当前弧优化&费用流)
欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...
- 最大流加强 dinic+当前弧优化
qyy开始练习网络流啦 , 啊 ,蒟蒻只会套版 ,很裸的题 , 我连题都不想发了 ,可以参考我的代码(虽然我也是看的别人的 #include <iostream> #include < ...
- 网络流SAP+gap+弧优化算法
poj1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54962 Accept ...
随机推荐
- postgre sql递归查询
WITH RECURSIVE r AS (SELECT * FROM [表] WHERE id = xxxunion ALLSELECT [表].* FROM [表], r WHERE [表]. ...
- MySQL为Null会导致5个问题,个个致命!
在正式开始之前,我们先来看下 MySQL 服务器的配置和版本号信息,如下图所示: "兵马未动粮草先行",看完了相关的配置之后,我们先来创建一张测试表和一些测试数据. -- 如果存在 ...
- Thymeleaf语法总结 | 笔记分享
Thymeleaf语法总结 一.Thymeleaf介绍 Thymeleaf是Spring boot推荐使用的模版引擎,直接以html显示,前后端可以很好的分离. 二.Thymeleaf语法(Thy ...
- 容器编排系统K8s之NetworkPolicy资源
前文我们了解了k8s的网络插件flannel的基础工作逻辑,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14225657.html:今天我们来聊一下k8s上 ...
- Ubuntu20.04编译ffmpeg
1.安装编译所需工具,GCC 2.安装yasm nasm yasm和nasm是两个编译器,编译ffmpeg需要用到 安装命令: sudo aptitude install yasm nasm 3.安装 ...
- vue 过滤器 filter 的使用
1.局部过滤器的使用 比如性别,订单状态的数据,后端一般返回数字来代替状态.以性别为模拟数据,0是未知,1是男,2是女. 直接数据渲染出来,只有012,没有性别 根据后端返回的int类型值,前端对数据 ...
- MySQL中Exists和In的使用
Exists关键字: exists表示存在,是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避 ...
- Mac上“您没有权限来打开应用程序”(Big Sur)
最近电脑更新了Macos的最新11版大苏尔 Big Sur.很快问题就出现了:安装某个软件的时候Key Gen打不开,提示您没有权限来打开应用程序,类似这样:https://zhuanlan.zhih ...
- LeetCode225 用队列实现栈
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- at定时任务
1)at是只执行一次,执行完后任务删除.at的守护进程atd会以后台模式运行,检查作业队列来运行.2)默认 atd每60秒巡逻一次,有作业时候,检查作业时间,如果和当前时间一样,就执行任务3)在使用a ...