会$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. github 常用

    1.创建KEY,这个文件生成完了后,要保存好公钥和私钥文件 ssh-keygen -t rsa -C "abc@mail.com" 2.github上添加ssh密钥 3.拷贝公钥信 ...

  2. JS高级. 02 面向对象、创建对象、构造函数、自定义构造函数、原型

    面向对象的三大特性: 封装 a)  把一些属性和方法装到一个对象里 2.  继承 a)  js中的继承是指:   一个对象没有一些方法和属性,而另一个对象有 把另一个个对象的属性和方法,拿过来自己用, ...

  3. 46.VUE学习之--组件之使用动态组件灵活设置页面布局

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

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

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

  5. pygame小游戏之坦克大战

    以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5  pycharm import pygame,sys,time from random ...

  6. python基础小知识,is和==的区别,编码和解码

    1.is和==的区别 1)id() 通过id()我们可以查看到一个变量表示的值在内存中的地址 >>> s1 = "Tanxu" >>> s2 = ...

  7. ecshop漏洞修复 以及如何加固ecshop网站安全?

    由于8月份的ECSHOP通杀漏洞被国内安全厂商爆出后,众多使用ecshop程序源码的用户大面积的受到了网站被篡改,最明显的就是外贸站点被跳转到一些仿冒的网站上去,导致在谷歌的用户订单量迅速下降,从百度 ...

  8. unity开发c#代码

    1.摄像头跟随主角移动,并支持旋转. 开发过程中需要摄像头以一定距离跟随player,同时会进行旋转,属于一种常见的跟随方式. using UnityEngine; using System.Coll ...

  9. Eclipse_安装SAP_HANA数据库插件

    1.对于Eclipse Oxygen,请添加URL https://tools.hana.ondemand.com/oxygen 2.对于Eclipse luna,请添加URL     https:/ ...

  10. java 第五章 方法定义及调用

    1.方法的定义 什么是方法 方法是完成某个功能的一组语句,通常将常用的功能写成一个方法 方法的定义 [访问控制符] [修饰符] 返回值类型 方法名( (参数类型 形式参数, ,参数类型 形式参数, , ...