会$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. sax技术解析xml下jaxp解析器详细代码

    *解析xml的两种技术dom和sax dom:根据xml的层级结构在内存中分配一个树形结构,把xml标签,属性,文本封装成对象. sax方式:事件驱动,边读边解析. 在javax.xml.parser ...

  2. 【linux基于Postfix和Dovecot邮件系统的搭建】

    一:PostFixe和Dovecot的简单介绍 Postfix postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.postfix是Wietse Ven ...

  3. React中的全选反选问题

    全选反选问题 1.在state里维护一个数组,例如showArr:[] 2.绑定点击事件的时候将当前这个当选按钮的index加进来 <span className='arrow' onClick ...

  4. Mina 组件介绍之 IoBuffer

    在Java NIO 中,ByteBuffer通常作为通信中传递消息的载体.而在Mina中,采用了IoBuffer代替ByteBuffer.Mina给出了不用ByteBuffer的两个主要理由: 1.  ...

  5. Pig关系型运算符例子

    1.新建两个文件A.txt与B.txt, A.txt文件的内容如下: 0,1,2 1,3,4 B.txt文件的内容如下: 0,5,2 1,7,8 将这两个文件上传到目录/zwy/soft 2.定义关系 ...

  6. keil5 mdk使用ST-Link II下载出现cannot halt the core解决办法

    在正常的程序里面,我添加了MB85RS16(spi flash)这个外设驱动代码后,使用ST-Link II下载就出现cannot halt the core. 这个现象之前出现过1次,但是解决办法忘 ...

  7. Leecode刷题之旅-C语言/python-104二叉树最大深度

    /* * @lc app=leetcode.cn id=104 lang=c * * [104] 二叉树的最大深度 * * https://leetcode-cn.com/problems/maxim ...

  8. 嵌入式框架Zorb Framework搭建二:环形缓冲区的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  9. Python3 os模块&sys模块&hashlib模块

    ''' os模块 非常重要的模块 ''' import os # print(os.getcwd()) # 获取当前工作目录 # os.chdir(r'路径名') # 改变当前工作目录 # print ...

  10. 笔记-twisted-adbapi-scrapy

    笔记-twisted-adbapi-scrapy-mysql 1.      异步插入mysql 在爬虫中需要insert到mysql,但有一个问题是在爬虫环境中commit的及时性与性能冲突. 一般 ...