网络流 - 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 ...
随机推荐
- 服务器运行jupyter,本地浏览器打开
https://www.cnblogs.com/wwwhza/p/8821117.html https://blog.csdn.net/qq_29762941/article/details/8063 ...
- C# 使用 log4net 日志组件
一. 什么是 log4net Apache log4net 库是帮助程序员将日志语句输出到各种输出目标的工具,它是从Java中的Log4j迁移过来的一个.Net版的开源日志框架.log4net 的一 ...
- 2020年Python文章盘点,我选出了个人TOP10
大家好,我是猫哥.2020年过得真快啊!总感觉这一年里还没有做成多少事,一眨眼就又到了写年度总结的时候了-- 去年1月1日的时候,我写了<我的 2019 年 Python 文章榜单>,简单 ...
- 翻译 | 30个 Python3 的最佳实践,技巧和窍门
1.使用 Python3 如果你关注 Python 的话,应该会知道 Python 2 已经于今年(2020 年)1 月 1 日正式弃用了.这份教程的很多例子都是只支持 Python 3 的,如果你还 ...
- CountDownLatch/CyclicBarrier/Semaphore 使用过吗
CountDownLatch 让一些线程堵塞直到另一个线程完成一系列操作后才被唤醒.CountDownLatch 主要有两个方法,当一个或多个线程调用 await 方法时,调用线程会被堵塞,其他线程调 ...
- 基于LDAP&&Role-based Authorization Strategy实现Jenkins团队权限管理
在实际工作中,存在多个团队都需要Jenkins来实现持续交付,但是又希望不同团队之间进行隔离,每个项目有自己的view, 只能看到自己项目的jenkins job. 但是,jenkins默认的权限管理 ...
- Eclipse-Che 安装(Centos)
安装docker,然后执行:docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock -v /home/cheData:/dat ...
- TypeScript接口与类的使用
一.TypeScript接口 Interfaces 可以约定一个对象的结构 一个对象去实现一个接口 就必须拥有这个接口中所有的成员用interface定义接口, 并且定义接口中成员的类型 编译之后会发 ...
- 9. 细节见真章,Formatter注册中心的设计很讨巧
目录 本文提纲 版本约定 你好,我是A哥(YourBatman). Spring设计了org.springframework.format.Formatter格式化器接口抽象,对格式化器进行了大一统, ...
- CAN总线采样点测试
采样点是什么? 采样点是接受节点判断信号逻辑的位置,CAN通讯属于异步通讯.需要通过不断的重新同步才能保证收发节点的采样准确. 若采样点太靠前,则因为线缆原因,DUT外发报文尚未稳定,容易发生采样错误 ...