• 这是初学网络流的时候从《算法竞赛进阶指南》抄下来的一份代码,自己理解的也不是很透彻。
  • 注意,边要从 \(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 + 当前弧优化【代码】的更多相关文章

  1. ARC085E(最小割规划【最大流】,Dinic当前弧优化)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...

  2. [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]

    题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...

  3. P3376 网络流-最大流模板题(Dinic+当前弧优化)

    (点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处 ...

  4. Dinic当前弧优化 模板及教程

    在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...

  5. HDU 4280 Island Transport(dinic+当前弧优化)

    Island Transport Description In the vast waters far far away, there are many islands. People are liv ...

  6. 【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)

    题目链接 妈耶 我的图建反了两次 准确的说是有两个地方建反了,然后反上加反改了一个小时…… 知道为什么要拆点吗? 假设这是你的图   左边到右边依次是超级源点    练习册     书     答案 ...

  7. 网络流小记(EK&dinic&当前弧优化&费用流)

    欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...

  8. 最大流加强 dinic+当前弧优化

    qyy开始练习网络流啦 , 啊 ,蒟蒻只会套版 ,很裸的题 , 我连题都不想发了 ,可以参考我的代码(虽然我也是看的别人的 #include <iostream> #include < ...

  9. 网络流SAP+gap+弧优化算法

    poj1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54962   Accept ...

随机推荐

  1. java中远程调用接口springboot

    package com.kakarote.crm.utils; import cn.hutool.core.util.ObjectUtil; import org.apache.http.client ...

  2. Java基础之String中equals,声明方式,等大总结

    无论你是一个编程新手还是老手,提到String你肯定感觉特别熟悉,因为String类我们在学习java基础的时候就已经学过,但是String类型有我们想象的那么简单吗?其实不然,String类型的知识 ...

  3. Liunx运维(七)-用户管理及用户信息查询命令

    文档目录: 一.useradd:创建用户 二.usermod:修改用户信息 三.userdel:删除用户 四.groupadd:创建新的用户组 五.groupdel:删除用户组 六.passwd:修改 ...

  4. 前端面试题归类-HTML2

    一. SGML . HTML .XML 和 XHTML 的区别? SGML 是标准通用标记语言,是一种定义电子文档结构和描述其内容的国际标准语言,是所有电子文档标记语言的起源. HTML 是超文本标记 ...

  5. JAVA_基础IO流对象流(三)

    处理流:对象流 ObjectInputStream和OjbectOutputSteam用于存储和读取基本数据类型数据或对象的处理流.可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来. ...

  6. MyArray框架搭建与实现

    #include<iostream> using namespace std; template<class T> class MyArray { public: //构造函数 ...

  7. Windows server 安装远程桌面及破解120天时间限制授权

    一.问题描述 Windows Server系列服务器默认远程桌面连接数是2个用户(本文适用于所有Windows Server系列服务器),如果多余两个用户进行远程桌面连接时,系统就会提示超过连接数,可 ...

  8. Vue3.0聊天室|vue3+vant3仿微信聊天实例|vue3.x仿微信app界面

    一.项目简介 基于Vue3.0+Vant3.x+Vuex4.x+Vue-router4+V3Popup等技术开发实现的仿微信手机App聊天实例项目Vue3-Chatroom.实现了发送图文表情消息/g ...

  9. Apache伪静态(Rewrite).htaccess文件详解

    Htaccess(超文本访问)是一个简单的配置文件,它允许设计师,开发者和程序员通过它来改变Apache Web服务器的配置.这些功能包括用户重定向.URL重写(url rewrite,国内很多称为伪 ...

  10. Linux下的screen和作业任务管理

    一.screen 首先介绍下screen,screen是Linux下的一个任务容器,开启了之后就可以让任务在后台执行而不会被网络中断或者是终端退出而影响到. 在Linux中有一些耗时比较久的操作(例如 ...