//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. djangoRestFrameWork的小知识

    djangoRestFrameWork的小知识 重写序列化器的save方法 有时候,.create()和.update()方法名称可能没有意义.例如,在联系表格中,我们可能没有创建新实例,而是发送了电 ...

  2. CSS3 制作正方体

    一.预备知识 变形属性 2D变形属性 transform:他是css3中的变形属性: 通过transform(变形) 来实现2d 或者3d 转换,其中2d 有,缩放 scale(x, y) ,移动 t ...

  3. AJ学IOS 之第一次打开Xcode_git配置,git简单学习

    AJ分享,必须精品 一:错误 当第一次打开Xcode我们进行commit操作的时候会报错: The working copy “测试” failed to commit files. * Please ...

  4. matlab计算样本熵

    计算14通道得脑电数据吗,将得出的样本熵插入Excel表格 a = zeros(1,14); b = a'; for i =1:14 b(i) = SampEn(d1_1(i,1:3000),2,0. ...

  5. commonJS、AMD和CMD之间的区别

    JS中的模块规范(CommonJS,AMD,CMD),如果你听过js模块化这个东西,那么你就应该听过或CommonJS或AMD甚至是CMD这些规范咯,我也听过,但之前也真的是听听而已. 现在就看看吧, ...

  6. 带权值的LCA

    例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...

  7. 14.移动端图片浏览组件 react-wx-images-viewer

    安装 npm install --save react-wx-images-viewer 使用 import WxImageViewer from 'react-wx-images-viewer'; ...

  8. [linux][mysql] 命令更改表结构:添加、删除、修改字段、调整字段顺序

    原文出处:http://www.phpernote.com/MySQL/1120.html 查看表结构: desc tabl_name; show columns fromtable_name: 常用 ...

  9. shiro:注解配置(五)

    基于[shiro集成spring]项目改造 引入相关依赖环境 <!--AOP的jar包--> <dependency> <groupId>org.aspectj&l ...

  10. python执行提示“ImportError: No module named OpenSSL.crypto”

    错误信息如下: Traceback (most recent call last): File "/usr/local/yunanbao/yxz-script/autoops/TaskSer ...