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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

会$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;
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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

Linux怎么添加mysql系统环境变量 - 开发技术

这篇文章主要讲解了“Linux怎么添加mysql系统环境变量”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux怎么添加mysql系统环境变量”吧!Linux添加mysql系统环境变量昨天安装完mysql后,直接在...

云计算导论(第2版)课后题答案

云计算课后习题答案 第1章 1.6 习题 1、美国国家标准与技术研究院NIST是如何定义云计算的 答案云计算是一种按使用量付费的模式这种模式提供可用的、便捷的、按需的网络访问 进入可配置的计算资源共享池资源包括网络、服务器、存储、应用软件、服务这些资源能够被快速提供只需投入很少的管理...

UVa 10916 Factstone Benchmark (数学&阶乘的处理技巧)

10916 - Factstone BenchmarkTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page...

UVa 573 The Snail (模拟)

573 - The SnailTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_proble...

php能不能往文档里面存数组 - 编程语言

本文小编为大家详细介绍“php能不能往文档里面存数组”,内容详细,步骤清晰,细节处理妥当,希望这篇“php能不能往文档里面存数组”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。 PHP 是一门脚本语言,常用于服务器...

SGU 107 987654321 problem (数论)_《数论》

time limit per test: 0.5 sec. memory limit per test: 4096 KBhttp://acm.sgu.ru/problem.php?contest=0&problem=107For given number N you must ou...