[洛谷P4722]【模板】最大流 加强版 / 预流推进
会$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]【模板】最大流 加强版 / 预流推进的更多相关文章
- 洛谷P2045 方格取数加强版(费用流)
题意 题目链接 Sol 这题能想到费用流就不难做了 从S向(1, 1)连费用为0,流量为K的边 从(n, n)向T连费用为0,流量为K的边 对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次, ...
- 洛谷 - P2045 - 方格取数加强版 - 费用流
原来这种题的解法是费用流. 从一个方格的左上走到右下,最多走k次,每个数最多拿走一次. 每次走动的流量设为1,起始点拆点成限制流量k. 每个点拆成两条路,一条路限制流量1,费用为价值相反数.另一条路无 ...
- 洛谷P4014 分配问题【最小/大费用流】题解+AC代码
洛谷P4014 分配问题[最小/大费用流]题解+AC代码 题目描述 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为c ij. 试设计一个将 n 件工作分配给 n 个人做的 ...
- 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...
- 洛谷P1120 小木棍 [数据加强版](搜索)
洛谷P1120 小木棍 [数据加强版] 搜索+剪枝 [剪枝操作]:若某组拼接不成立,且此时 已拼接的长度为0 或 当前已拼接的长度与刚才枚举的长度之和为最终枚举的答案时,则可直接跳出循环.因为此时继续 ...
- 洛谷-P5357-【模板】AC自动机(二次加强版)
题目传送门 -------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题 sol:AC自动机,还是要解决跳fail边产生的重复访问,但 ...
- 图论--网络流--最大流 洛谷P4722(hlpp)
题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...
- 洛谷 P4016负载平衡问题【费用流】题解+AC代码
洛谷 P4016负载平衡问题 P4014 分配问题[费用流]题解+AC代码 负载平衡问题 题目描述 GG 公司有n个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n ...
- 洛谷P3375 [模板]KMP字符串匹配
To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...
随机推荐
- java初级应用:环境安装及配置
相关文件下载: jdk下载路径:http://www.oracle.com/technetwork/java/javase/downloads/index.html eclipse下载路径:https ...
- pygame小游戏之坦克大战
以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5 pycharm import pygame,sys,time from random ...
- 素数环 南阳acm488(回溯法)
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling
题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...
- 0301001_Lesson1&2
Lesson 1 Excuse me! 对不起! Listen to the tape then answer this question.Whose handbag is it?听录音,然后回答问题 ...
- 类 java.util.Collections 提供了对Set、List、Map进行排序、填充、查找元素的辅助方法。
类 java.util.Collections 提供了对Set.List.Map进行排序.填充.查找元素的辅助方法. 1. void sort(List) //对List容器内的元素排序,排序的规 ...
- 「日常训练」「小专题·USACO」 Barn Repair(1-4)
题意 之后补. 分析 这题同样也很精巧.我们不妨思考一下,如果只允许用一块木板,那么要购买多少距离?是整个的距离吗?不是,是从第一个到最后一个(哈哈哈哈哈哈哈).但是,不包括第一个的"左边& ...
- 使用IDEA快速搭建Gradle项目
- Qt Qwdget 汽车仪表知识点拆解5 标题栏图标闪烁
先贴上效果图,注意,没有写逻辑,都是乱动的 看下最上面的部分,有一些仪表图标在闪烁,如果一个一个写,也是可以实现的,不过感觉要累死的节奏 这里我写了一个我自己的Label,完了把把这些QLabel提升 ...
- 51单片机实现外部中断0-F
#include< reg51.h> #define uint unsigned int #define uchar unsigned char sfr P0M0 = 0x94; sfr ...