复杂度上界为 \(\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(多路增广+当前弧优化)的更多相关文章

  1. P3376 [模板] 网络最大流

    https://www.luogu.org/blog/ONE-PIECE/wang-lao-liu-jiang-xie-zhi-dinic EK 292ms #include <bits/std ...

  2. P3376 网络最大流模板(Dinic + dfs多路增广优化 + 炸点优化 + 当前弧优化)

    ### P3376 题目链接 ### 这里讲一下三种优化的实现以及正确性. 1.dfs多路增广优化 一般的Dinic算法中是这样的,bfs() 用于标记多条增广路,以至于能一次 bfs() 出多次 d ...

  3. bzoj 5120 无限之环 & 洛谷 P4003 —— 费用流(多路增广SPFA)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5120 https://www.luogu.org/problemnew/show/P4003 ...

  4. LOJ 2979 「THUSCH 2017」换桌——多路增广费用流

    题目:https://loj.ac/problem/2979 原来的思路: 优化连边.一看就是同一个桌子相邻座位之间连边.相邻桌子对应座位之间连边. 每个座位向它所属的桌子连边.然后每个人建一个点,向 ...

  5. P3376 【模板】网络最大流 dinic详解

    dinic的核心在于分层和多路增广. 分层的意思是,对于图用bfs搜出每一层,避免出现dfs深度过深的情况. 多路增广,利用的是dfs的回溯性质,这样就可以在一个点增广出它的所有流量. #includ ...

  6. 网络最大流 Dinic算法

    前言 看到网上好多都用的链式前向星,就我在用 \(vector\) 邻接表-- 定义 先来介绍一些相关的定义.(个人理解) 网络 一个网络是一张带权的有向图 \(G=(V,E)\) ,其中每任意一条边 ...

  7. P3376 【模板】网络最大流dinic算法

    P3376 [模板]网络最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点 ...

  8. 网络流初步:<最大流>——核心(增广路算法)(模板)

    增广路的核心就是引入了反向边,使在进行道路探索选择的时候增加了类似于退路的东西[有一点dp的味道??] 具体操作就是:1.首先使用结构体以及数组链表next[ MAXN ]进行边信息的存储 2.[核心 ...

  9. luogu3376 【模板】网络最大流 dinic

    当前弧优化 #include <iostream> #include <cstring> #include <cstdio> #include <queue& ...

随机推荐

  1. offset新探索:双管齐下,加速大数据量查询

    摘要:随着offset的增加,查询的时长也会越来越长.当offset达到百万级别的时候查询时长通常是业务所不能容忍的. 本文分享自华为云社区<offset新探索:双管齐下,加速大数据量查询> ...

  2. 【MySQL】Navicat15 安装

    # Navicat安装` 提示`:鉴于之间已经出了MySQL的安装教程,在这了我也讲下,那个其实包含了两个知识点,既可以小白初次安装MySQL客户端,也面向想安装5.x和8.x两个版本的. --- @ ...

  3. GY91(MPU9250 + BMP280)惯性传感器开发指南

    目录 参考资料 I2C 设备ID 关键数据读取 MPU6500:读取加速度数据&换算单位 BMP280: 读取温度和气压信息 & 单位换算 推荐库 参考资料 参考资料说明: 用户手册时 ...

  4. HPL Study 1

    1.安装Linux系统 在虚拟机Vmware上安装CentOS 7系统 2.安装OneApi 安装的时候将文件从桌面拖动到虚拟机安装的时候报错:archive corrupted 解决方法:大文件应采 ...

  5. Python标准库之 xml.etree.ElementTree

    Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. 每个element对象都具有以下属性: 1. tag:string对象,表示数据代表的种类. 2. attrib:dictiona ...

  6. JS数据结构与算法-队列结构

    队列结构 一.认识队列 受限的线性结构: 我们已经学习了一种受限的线性结构:栈结构. 并且已经知道这种受限的数据结构对于解决某些特定问题,会有特别的 效果. 下面,我们再来学习另外一个受限的数据结构: ...

  7. 第2-1-5章 docker安装MinIO实现文件存储服务-springboot整合minio-minio全网最全的资料

    目录 1. MinIO介绍 2. MinIO应用场景 2.1 单主机单硬盘模式 2.2 单主机多硬盘模式 2.3 多主机多硬盘分布式 3. MinIO特点 4. 存储机制 5. docker安装Min ...

  8. kubernetes笔记-3-快速入门

    一.增删改查 root@master:~# kubectl run ninig-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dr ...

  9. springboot接收前端传参的几种方式

    1.通过HttpServletRequest接收,常用于获取请求头参数以及Cookie,适用于GET 和 POST请求方式,以下两种方式: @GetMapping("/demo1" ...

  10. JavaEE Day08 HTML&CSS

    今日内容 HTML标签:表单标签 CSS:页面样式控制,美化页面,完成页面布局 一.表单标签 1.概述 用于采集用户输入数据的,如输入的用户名和密码,用于与服务器进行交互 使用from标签  form ...