会$TLE$。。。

C++ Code:(HLPP)

#pragma GCC optimize(3)
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define maxn 1210
#define maxm 120010
const int inf = 0x3f3f3f3f;
inline int min(int a, int b) {return a < b ? a : b;}
namespace Network_Flow {
int st, ed, MF, n;
int head[maxn], cnt = 2;
struct Edge {
int to, nxt, w;
} e[maxm << 1];
inline void addE(int a, int b, int c) {
e[cnt] = (Edge) {b, head[a], c}; head[a] = cnt;
e[cnt ^ 1] = (Edge) {a, head[b], 0}; head[b] = cnt ^ 1;
cnt += 2;
}
bool inq[maxn];
int GAP[maxn << 1], h[maxn];
int prs[maxn];
struct cmp {
inline bool operator () (int a, int b) const {return h[a] < h[b];}
};
std::priority_queue<int, std::vector<int>, cmp> q;
inline bool N(int p) {return p != st && p != ed;}
inline void Push(int u) {
const int H = h[u] - 1;
for (register int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (e[i].w && h[v] == H) {
int w = min(prs[u], e[i].w);
e[i].w -= w, e[i ^ 1].w += w;
prs[u] -= w, prs[v] += w;
if (v != st && v != ed && !inq[v]) {
q.push(v);
inq[v] = true;
}
if (!prs[u]) return ;
}
}
}
inline void Gap(int H) {
for (register int i = 1; i <= n; i++) if (N(i) && H < h[i] && h[i] <= n) h[i] = n + 1;
}
namespace BFS {
int Q[maxn], H, T;
inline bool bfs() {
memset(h, 0x3f, sizeof h);
h[Q[H = T = 0] = ed] = 1;
while (H <= T) {
int u = Q[H++];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (e[i ^ 1].w && h[v] > h[u] + 1) {
h[v] = h[u] + 1;
Q[++T] = v;
}
}
}
return h[ed] != inf;
}
}
inline void relabel(int u) {
int &H = h[u] = inf;
for (register int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (e[i].w && h[v] + 1 < H) H = h[v] + 1;
}
}
inline void HLPP(int __S, int __T) {
st = __S, ed = __T;
if (!BFS::bfs()) return ;
h[st] = n;
for (register int i = 1; i <= n; i++) if (h[i] < inf) GAP[h[i]]++;
for (register int i = head[st]; i; i = e[i].nxt) {
int v = e[i].to, &w = e[i].w;
if (!w) continue;
e[i ^ 1].w += w, prs[st] -= w, prs[v] += w;
w = 0;
if (v != ed && !inq[v]) {
q.push(v);
inq[v] = true;
}
}
while (!q.empty()) {
int u = q.top(); q.pop();
inq[u] = false; Push(u);
if (!prs[u]) continue;
if (!--GAP[h[u]]) for (register int i = 1; i <= n; i++) if (i != st && i != ed && h[u] < h[i] && h[i] <= n) h[i] = n + 1; //Gap(h[u]);
relabel(u); GAP[h[u]]++;
q.push(u); inq[u] = true;
}
MF = prs[ed];
}
inline void clear() {
memset(head, 0, sizeof head); cnt = 2;
memset(GAP, 0, sizeof GAP); memset(prs, 0, sizeof prs);
while (!q.empty()) q.pop();
MF = 0;
}
}
#define read() R::READ()
#include <cctype>
namespace R {
int x;
#ifdef ONLINE_JUGER
#define M 1 << 25
char op[M], *ch;
inline void init() {fread(ch = op, 1, M, stdin);}
inline int READ() {
while (isspace(*ch)) ch++;
for (x = *ch & 15, ch++; isdigit(*ch); ch++) x = x * 10 + (*ch & 15);
return x;
}
#undef M
#else
char ch;
inline int READ() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
#endif
}
int n, m, st, ed;
int main() {
#ifdef ONLINE_JUGER
R::init();
#endif
Network_Flow::n = read(), m = read(), st = read(), ed = read();
for (register int i = 0, a, b, c; i < m; i++) {
a = read(), b = read(), c = read();
Network_Flow::addE(a, b, c);
}
Network_Flow::HLPP(st, ed);
printf("%d\n", Network_Flow::MF);
return 0;
}

[洛谷P4722]【模板】最大流 加强版 / 预流推进的更多相关文章

  1. 洛谷P2045 方格取数加强版(费用流)

    题意 题目链接 Sol 这题能想到费用流就不难做了 从S向(1, 1)连费用为0,流量为K的边 从(n, n)向T连费用为0,流量为K的边 对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次, ...

  2. 洛谷 - P2045 - 方格取数加强版 - 费用流

    原来这种题的解法是费用流. 从一个方格的左上走到右下,最多走k次,每个数最多拿走一次. 每次走动的流量设为1,起始点拆点成限制流量k. 每个点拆成两条路,一条路限制流量1,费用为价值相反数.另一条路无 ...

  3. 洛谷P4014 分配问题【最小/大费用流】题解+AC代码

    洛谷P4014 分配问题[最小/大费用流]题解+AC代码 题目描述 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为c ij. 试设计一个将 n 件工作分配给 n 个人做的 ...

  4. 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)

    To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...

  5. 洛谷P1120 小木棍 [数据加强版](搜索)

    洛谷P1120 小木棍 [数据加强版] 搜索+剪枝 [剪枝操作]:若某组拼接不成立,且此时 已拼接的长度为0 或 当前已拼接的长度与刚才枚举的长度之和为最终枚举的答案时,则可直接跳出循环.因为此时继续 ...

  6. 洛谷-P5357-【模板】AC自动机(二次加强版)

    题目传送门 -------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题 sol:AC自动机,还是要解决跳fail边产生的重复访问,但 ...

  7. 图论--网络流--最大流 洛谷P4722(hlpp)

    题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...

  8. 洛谷 P4016负载平衡问题【费用流】题解+AC代码

    洛谷 P4016负载平衡问题 P4014 分配问题[费用流]题解+AC代码 负载平衡问题 题目描述 GG 公司有n个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n ...

  9. 洛谷P3375 [模板]KMP字符串匹配

    To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...

随机推荐

  1. redis搭建

    redis 1.简介.安装 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.redis 和 Memcached 缓存服务很像 ...

  2. 06 python操作MySQL和redis(进阶)

    python操作mysql.redis 阶段一.mysql事务 主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息, ...

  3. 一起来学习Shell脚本

    Shell脚本 Shell脚本(shell script),是一种为shell编写的脚本程序. 大家所说的shell通常都是指的shell脚本,但其实shell与shell脚本是两个不同的概念.由于习 ...

  4. Android面试收集录 Android系统的资源+其他

    1.Android应用程序的资源是如何存储的,如何使用? res文件夹或者assets文件夹 res目录中的资源在R类中生成一个int变量,然后再布局文件中可以直接使用,在代码中,要getResour ...

  5. Java - 问题集 - 导出csv文件中文乱码

    微软的excel文件需要通过文件头的bom来识别编码,所以写文件时,需要先写入bom头. FileOutputStream fos = new FileOutputStream(new File(&q ...

  6. (原)编写JAVA工具之json自动封装成pojo

    代码在最后 我个人是不太喜欢http和json,可能是游戏做的多了的原因的,对通信协议和通信方式特敏感,因此即使是做应用我也会选择rpc而非http,但是有时候因为各种原因,还是不的不处理标准的htt ...

  7. 「日常训练」Our Tanya is Crying Out Loud (CFR466D2B)

    题意(Codeforces 940B) 对一个数字$x$,你有两个决策:花费$A$减一.或花费$B$除以$k$(但必须可以除尽).问使之到$1$的最少花费. 分析 贼鸡儿简单,但我花式犯蠢……如果除不 ...

  8. 【题解搬运】PAT_L1-009 N个数求和

    从我原来的博客上搬运.原先blog作废. (伪)水题+1,旨在继续摸清这个blog(囧 题目 就是求N个数字的和.麻烦的是,这些数字是以有理数"分子/分母"的形式给出的,你输出的和 ...

  9. vue之vue-cookies使用

    一.安装vue-cookies npm install --save vue-cookies 或者 yarn add vue-cookies 二.引入vue-cookie // 方式一:require ...

  10. 不得不服!Python速度虽然慢,但是它工作效率很高!

    写在前面 让我们来讨论一个我最近一直在思考的问题:Python 的性能.顺便说一下,我是 Python 的忠实拥趸,我在各种情况下都会积极尝试使用 Python 来解决问题.大家对 Python 最大 ...