//500ms 秒掉洛谷推流问题
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
typedef long long F_type;
const int MAXN = 1.2e3 + 10, INF = 0x3f3f3f3f;
const LL LINF = (LL)INF << 32 | INF;
struct Edge
{
int v, rev;
F_type cap;
Edge(int a, F_type b, int c) : v(a), rev(c), cap(b) {}
};
const F_type maxf=LINF;
F_type exflow[MAXN];
int h[MAXN], cnt[MAXN];
int ht, N, S, T, labelcnt;
vector<Edge> G[MAXN];
vector<int> hq[MAXN];
void clear(int n = MAXN - 1)
{
ht = labelcnt = 0;
for (int i = 0; i <= n; i++)
G[i].clear();
}
void addEdge(int u, int v, F_type cap)
{
G[u].emplace_back(v, cap, G[v].size());
G[v].emplace_back(u, 0, G[u].size() - 1);
}
void update(int u, int newh)
{
++labelcnt;
if (h[u] != N + 1)
--cnt[h[u]];
h[u] = newh;
if (newh == N + 1)
return;
++cnt[ht = newh];
if (exflow[u] > 0)
hq[newh].push_back(u);
}
void globalRelabel()
{
queue<int> q;
for (int i = 0; i <= N + 1; i++)
hq[i].clear();
for (int i = 0; i <= N; i++)
h[i] = N + 1, cnt[i] = 0;
q.push(T);
labelcnt = ht = h[T] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
for (Edge& e : G[u])
{
if (h[e.v] == N + 1 && G[e.v][e.rev].cap)
{
update(e.v, h[u] + 1);
q.push(e.v);
}
}
ht = h[u];
}
}
void push(int u, Edge& e)
{
if (exflow[e.v] == 0)
hq[h[e.v]].push_back(e.v);
F_type df = min(exflow[u], e.cap);
e.cap -= df;
G[e.v][e.rev].cap += df;
exflow[u] -= df;
exflow[e.v] += df;
}
void discharge(int u)
{
int nxth = N + 1;
for (Edge& e : G[u])
if (e.cap)
{
if (h[u] == h[e.v] + 1)
{
push(u, e);
if (exflow[u] <= 0)
return;
}
else
nxth = min(nxth, h[e.v] + 1);
}
if (cnt[h[u]] > 1)
update(u, nxth);
else
for (; ht >= h[u]; hq[ht--].clear())
{
for (int& j : hq[ht])
update(j, N + 1);
}
}
F_type maxFlow(int s, int t, int n)
{
S = s, T = t, N = n;
memset(exflow, 0, sizeof(exflow));
exflow[S] = maxf;
exflow[T] = -maxf;
globalRelabel();
for (Edge& e : G[S])
push(S, e);
for (; ht >= 0; --ht)
{
while (!hq[ht].empty())
{
int u = hq[ht].back();
hq[ht].pop_back();
discharge(u);
if (labelcnt > (N << 2))
globalRelabel();
}
}
return exflow[T] + maxf;
} int main()
{
int n, m, s, t, u, v, w;
scanf("%d%d%d%d", &n, &m, &s, &t);
while (m--)
{
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
}
printf("%d", maxFlow(s, t, n));
return 0;
}

网络流--最大流--hlpp(预流推进)模板的更多相关文章

  1. [洛谷P4722]【模板】最大流 加强版 / 预流推进

    会$TLE$... C++ Code:(HLPP) #pragma GCC optimize(3) #pragma GCC optimize("unroll-loops") #in ...

  2. 最大流算法-最高标号预流推进(HLPP)

    昨天我们学习了ISAP算法,它属于增广路算法的大类.今天学习的算法是预流推进算法中很高效的一类--最高标号预流推进(HLPP). 预流推进 预流推进是一种很直观的网络流算法.如果给到一个网络流让你手算 ...

  3. ZOJ-2364 Data Transmission 分层图阻塞流 Dinic+贪心预流

    题意:给定一个分层图,即只能够在相邻层次之间流动,给定了各个顶点的层次.要求输出一个阻塞流. 分析:该题直接Dinic求最大流TLE了,网上说采用Isap也TLE,而最大流中的最高标号预流推进(HLP ...

  4. Libre 6013 「网络流 24 题」负载平衡 (网络流,最小费用最大流)

    Libre 6013 「网络流 24 题」负载平衡 (网络流,最小费用最大流) Description G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使n ...

  5. Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流)

    Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流) Description W 公司有m个仓库和n个零售商店.第i个仓库有\(a_i\)个单位的货物:第j个零售商店需要\( ...

  6. Libre 6010「网络流 24 题」数字梯形 (网络流,最大费用最大流)

    Libre 6010「网络流 24 题」数字梯形 (网络流,最大费用最大流) Description 给定一个由n 行数字组成的数字梯形如下图所示.梯形的第一行有m 个数字.从梯形的顶部的m 个数字开 ...

  7. Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流)

    Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流) Description 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,-,N).餐厅可以从三种途径获得餐巾. ...

  8. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  9. .net 流(Stream) - 文件流、内存流、网络流

    转自:http://www.oseye.net/user/kevin/blog/85 一.文件流 FileStream FileStream流继承与Stream类,一个FileStream类的实例实际 ...

  10. [ZOJ2341]Reactor Cooling解题报告|带上下界的网络流|无源汇的可行流

    Reactor Cooling The terrorist group leaded by a well known international terrorist Ben Bladen is bul ...

随机推荐

  1. tornado自定义实现django include方法

    tornado自定义实现django  include方法 自定义URLmethod模块 from Custom.errors import * def include(base_url, expan ...

  2. BaseRuntimeException自定义业务异常类实现

    public class BaseRuntimeException extends RuntimeException { private final int code; public BaseRunt ...

  3. curl 交叉编译 支持http2和openssl

    touch run.sh chmod 755 run.sh mkdir build cd build ../run.sh run.sh #!/bin/bash #cd /build ../config ...

  4. nghttp2 交叉编译

    touch run.sh chmod 755 run.sh mkdir build cd build ../run.sh run.sh #!/bin/bash #cd build ../configu ...

  5. Python 应用领域及学习重点

    笔者认为不管学习什么编程语言,首先要知道:学完之后在未来能做些什么? 本文将浅谈 Python 的应用领域及其在对应领域的学习重点.也仅是介绍了 Python 应用领域的"冰山一角" ...

  6. 搞懂 XML 解析,徒手造 WEB 框架

    恕我斗胆直言,对开源的 WEB 框架了解多少,有没有尝试写过框架呢?XML 的解析方式有哪些?能答出来吗?! 心中没有答案也没关系,因为通过今天的分享,能让你轻松 get 如下几点,绝对收获满满. a ...

  7. Python 小技之实现的鲜花盛宴,你准备好了吗?

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:派森酱 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...

  8. L5语言模型与数据集

    本次实验使用的数据下载: jaychou_lyrics.txt 链接:https://pan.baidu.com/s/1LJSrkpV84YF61OPmjIHGIw 提取码:dj53 语言模型 一段自 ...

  9. C++枚举算法

    枚举算法 什么是枚举? 枚举,顾名思义,就是用最笨的方法,去解决问题(暴力枚举),一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数.这两种类型经常(但不总是)重叠. 枚举 ...

  10. 2020-MRCTF

    ez_bypass I put something in F12 for you include 'flag.php'; $flag='MRCTF{xxxxxxxxxxxxxxxxxxxxxxxxx} ...