【模板】网络最大流 Dinic(多路增广+当前弧优化)
复杂度上界为 \(\Theta(n^2m)\),实际效率远高于此。
#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;
const int M=1e6+5;
const int MN=1e3+5;
typedef long long ll;
inline int read(){int x(0), f(0); char ch=getchar(); while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48); ch=getchar();} return f?-x:x;}
template <typename T> void read(T &x){x=0; T f(0); char ch=getchar(); while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48); ch=getchar();} x=f?-x:x;}
template <typename T,typename ...Arg>void read(T& x,Arg& ...arg){read(x);read(arg...);}
template <typename T> inline void write(T x){static char buf[64]; static int tot(0); if(x<0) putchar('-'),x=-x; do buf[++tot]=(x%10)+48,x/=10; while(x); do putchar(buf[tot--]); while(tot);}
template <typename T> void write(T x,char c){static char buf[64]; static int tot(0); if(x<0) putchar('-'),x=-x; do buf[++tot]=(x%10)+48,x/=10; while(x); do putchar(buf[tot--]); while(tot); putchar(c);}
void Solve();
struct Graph{
int to, next;
ll w;
}G[M << 1];
int _head[N], cur[N], cnt(1);
void addEdge(int u,int v,int w = 1){G[++cnt] = (Graph){v, _head[u], w}; _head[u] = cnt;}
int n,m,s,t;
int main(){
// Multicase()
Solve();
}
int dep[N],que[N];
bool bfs(){
memcpy(cur + 1, _head + 1, (n + 5) * sizeof(cur[0]));
memset(dep, 0, (n + 5) * sizeof(dep[0]));
dep[s] = 1;
int head = 1, tail = 0; que[++tail] = s;
while(tail >= head){
int u = que[head++];
for(int i = _head[u]; i; i = G[i].next){
int t = G[i].to;
if(G[i].w && !dep[t]){
dep[t] = dep[u] + 1;
que[++tail] = t;
}
}
}
return dep[t];
}
ll dfs(int now, ll flow){
ll tot = 0, f = 0;
if(now == t || flow == 0) return flow;
for(int i = cur[now]; i; i = G[i].next){
cur[now] = i;
int t = G[i].to;
if(G[i].w && dep[t] == dep[now] + 1){
if(f = dfs(t, min(flow, G[i].w))){
G[i].w -= f;
G[i ^ 1].w += f;
tot += f;
flow -= f;
if(flow == 0) break;
}
}
}
return tot;
}
ll dinic(){
ll maxflow = 0;
while(bfs()) maxflow += dfs(s, 1<<30);
return maxflow;
}
void Solve(){
read(n, m);
s = 1, t = n;
for(int i = 1; i <= m; i++){
int u, v, w;
read(u, v, w);
addEdge(u, v, w);
addEdge(v, u, 0);
}
printf("%lld\n", dinic());
}
【模板】网络最大流 Dinic(多路增广+当前弧优化)的更多相关文章
- P3376 [模板] 网络最大流
https://www.luogu.org/blog/ONE-PIECE/wang-lao-liu-jiang-xie-zhi-dinic EK 292ms #include <bits/std ...
- P3376 网络最大流模板(Dinic + dfs多路增广优化 + 炸点优化 + 当前弧优化)
### P3376 题目链接 ### 这里讲一下三种优化的实现以及正确性. 1.dfs多路增广优化 一般的Dinic算法中是这样的,bfs() 用于标记多条增广路,以至于能一次 bfs() 出多次 d ...
- bzoj 5120 无限之环 & 洛谷 P4003 —— 费用流(多路增广SPFA)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5120 https://www.luogu.org/problemnew/show/P4003 ...
- LOJ 2979 「THUSCH 2017」换桌——多路增广费用流
题目:https://loj.ac/problem/2979 原来的思路: 优化连边.一看就是同一个桌子相邻座位之间连边.相邻桌子对应座位之间连边. 每个座位向它所属的桌子连边.然后每个人建一个点,向 ...
- P3376 【模板】网络最大流 dinic详解
dinic的核心在于分层和多路增广. 分层的意思是,对于图用bfs搜出每一层,避免出现dfs深度过深的情况. 多路增广,利用的是dfs的回溯性质,这样就可以在一个点增广出它的所有流量. #includ ...
- 网络最大流 Dinic算法
前言 看到网上好多都用的链式前向星,就我在用 \(vector\) 邻接表-- 定义 先来介绍一些相关的定义.(个人理解) 网络 一个网络是一张带权的有向图 \(G=(V,E)\) ,其中每任意一条边 ...
- P3376 【模板】网络最大流dinic算法
P3376 [模板]网络最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点 ...
- 网络流初步:<最大流>——核心(增广路算法)(模板)
增广路的核心就是引入了反向边,使在进行道路探索选择的时候增加了类似于退路的东西[有一点dp的味道??] 具体操作就是:1.首先使用结构体以及数组链表next[ MAXN ]进行边信息的存储 2.[核心 ...
- luogu3376 【模板】网络最大流 dinic
当前弧优化 #include <iostream> #include <cstring> #include <cstdio> #include <queue& ...
随机推荐
- Windows7下驱动开发与调试体系构建——4.在x64下使用汇编代码(x86下的_asm)
目录/参考资料:https://www.cnblogs.com/railgunRG/p/14412321.html asm文件设置 在vs x64中无法使用_asm关键字,需要使用.asm文件. 按第 ...
- Spring Core rce漏洞分析(CVE-2022-22965)
漏洞描述: Springmvc框架参数绑定功能,绑定了请求里的参数造成变量注入,攻击者可以实现任意文件写入,漏洞点spring-beans包中. 漏洞编号: CVE-2022-22965 影响范围: ...
- 只能用于文本与图像数据?No!看TabTransformer对结构化业务数据精准建模
作者:韩信子@ShowMeAI 深度学习实战系列:https://www.showmeai.tech/tutorials/42 TensorFlow 实战系列:https://www.showmeai ...
- Sprint产品待办列表的优先级要怎么排?
在梳理产品待办事项列表的过程中,产品负责人需要先做优先级排列,保证我们 在一定的时间盒内能够交付需要优先级最高.最具价值的用户故事. 那这个用户故事的优先级要怎么排列,我们怎样选择用户故事的实现顺序? ...
- 一个 MySQL 隐式转换的坑,差点把服务器整崩溃了
我是风筝,公众号「古时的风筝」,专注于 Java技术 及周边生态. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 本来是一个平静而美好的下午,其 ...
- 嵌入式-c语言基础:冒泡排序实现从大到小排列
#include<stdio.h> int main() { /*冒泡排序:从大到小*/ /*i=0 第1轮(i+1):需要比较9次(sizeArr-i-1)*/ /*i=1 第2轮(i+ ...
- JAVA系列之类加载机制详解
类的加载机制 ? 双亲委派机制 ? 什么是类加载器 ? 自定义类加载器有哪些应用场景 ? 通常,在关于Java的类加载部分会遇到以上疑问,本文将对类加载重要部分做详细介绍,包括重要的基础概念和应用场景 ...
- 【深入浅出 Yarn 架构与实现】3-2 Yarn Client 编写
上篇文章介绍了编写 Yarn Application 的整体框架流程,本篇文章将详细介绍其中 Client 部分的编写方式. 一.Yarn Client 编写方法 本篇代码已上传 Github: Gi ...
- 基于python的数学建模---场线与数值解(微分方程)
import numpy as np from scipy import integrate import matplotlib.pyplot as plt import sympy def plot ...
- os模块、sys模块、json模块、json模块实战
目录 os模块 创建目录(文件夹) 删除目录(文件夹) 列举指定路径下内容名称 删除/重命名文件 获取/切换当前工作目录 动态获取项目根路径(重要) 判断路径是否存在(文件.目录) 路径拼接(重要) ...