P9902 『PG2』模拟最大流 题解
前言
本题的本意应该是使用状压 DP,在时间复杂度
因为不难发现,本题数据量很大,但边权很小。
于是我们考虑使用最大流算法 AC。
本题解意义并不是乱搞靠卡常 AC,而是优化最大流算法。
正文
这是一道紫题,不可能让你用 Edmond_Karp 或者是 Dinic 和 ISAP 这类算法通过的。我们也不在这里讨论。绝不是我不会优化。
翻开算法库,还有一种算法,时间复杂度更优。那就是最高标号预流推进算法 HLPP,时间复杂度
如果你不知道,可以看这道题。P4722 【模板】最大流 加强版 / 预流推进
就算你用了,但你会发现,还是会 TLE。
请检查你在上面题目中总用时,如果是 5s,那肯定没戏,如果是 500ms,那也很难,要优化到 245ms,才到位。
HLPP
回顾 HLPP 算法的本质,按照节点高度,进行超额推流。
源点超额流是
除源点外,节点高度
显然会有两种情况。一种,超额流被推完了,结束推流;一种,超额流没有被推完,此时就不能再奢求以当前高度推流,只能在还有剩余容量的边中重新计算高度,重新推流。
优化一 全局标签重贴
如果大量超额流被推送出去,当推到后面发现推不出去了,此时只能通过修改节点高度回流。
但节点高度不是一次修改到位,而是节点之间反复推流,不断增加高度。这可太浪费了。因此我们使用全局标签重贴,每过一会儿,就直接重新设置节点高度,一次修改到位。
一次全局标签重贴是时间复杂度是
优化二 当前弧优化
容易发现不饱和推流对算法影响较小,不用修改高度。明显,如果一次不饱和推流从自己的边 1 推到边 i,那么
事实上,如果不加当前弧优化,时间复杂度会退化,加了才正确。
注意,如果是饱和推流,修改高度后,要重设当前弧。
优化三 gap 优化
超额流的推进一定是一步一个台阶,有序向下的。但如果楼梯断了,显然断层上的楼梯都没有用处了,此时直接废弃它们,能够节省很多时间。
优化四 桶写
如果使用堆进行维护等待推流的节点,时间复杂度会多一个
同时,高度超过 n 的节点就不再维护,等于被踢出算法,这大概能减少一点常数。
优化五 存图
使用 std::vector 进行存图会更快,这大概是因为内存访问更连续,速度更快。
可能是因为过度使用 STL,不开 O2 还是卡常,但只要开 O2,运行效率不亚于 DP 做法。
Code
#include <bits/stdc++.h>
//using namespace std;
struct HLPP {
public:
typedef int ANS_TYPE;
struct edge {
int to, rev;
ANS_TYPE flow;
edge(int to, ANS_TYPE flow, int rev): to(to), flow(flow), rev(rev) {}
edge() {}
};
int n, m, s, t, maxh, maxgaph, workcnt;
std::vector< std::vector<edge> >vec;
std::vector<ANS_TYPE>ov;
std::vector<int>h, cur, ovlist, ovnex, gap, gapprv, gapnex;
HLPP(int n, int m, int s, int t): n(n), m(m), s(s), t(t), maxh(0), maxgaph(0), workcnt(0),
vec(n + 1), ov(n + 1), h(n + 1), cur(n + 1),
ovlist((n + 1), -1), ovnex((n + 1), -1),
gap((n + 1), -1), gapprv((n + 1), -1), gapnex((n + 1), -1) {}
HLPP() {}
inline void addedge(int u, int v, ANS_TYPE f) {
vec[u].push_back(edge(v, f, vec[v].size()));
vec[v].push_back(edge(u, 0, vec[u].size() - 1));
}
inline ANS_TYPE get_max_flow() {
relabel();
for (auto &e : vec[s]) {
if (e.flow) {
push_flow(s, e, e.flow);
maxh = std::max(maxh, h[e.to]);
}
}
for (; maxh; --maxh) {
while (~ovlist[maxh]) {
int x = ovlist[maxh];
ovlist[maxh] = ovnex[x];
update_dis(x);
if (workcnt >= (n << 2)) {
relabel();
}
}
}
return ov[t];
}
inline void update_dis(int x) {
int nh = n, sz = vec[x].size();
for (int i = cur[x]; i < sz; ++i) {
auto &e = vec[x][i];
if (e.flow) {
if (h[x] == h[e.to] + 1) {
push_flow(x, e, std::min(ov[x], e.flow));
if (!ov[x]) {
cur[x] = i;
return;
}
} else {
nh = std::min(nh, h[e.to] + 1);
}
}
}
for (int i = 0; i < cur[x]; ++i) {
auto &e = vec[x][i];
if (e.flow) {
nh = std::min(nh, h[e.to] + 1);
}
}
cur[x] = 0;
++workcnt;
if (~gapnex[gap[h[x]]]) {
set_height(x, nh);
} else {
int oldh = h[x];
for (int i = oldh; i <= maxgaph; ++i) {
for (int j = gap[i]; ~j; j = gapnex[j]) {
h[j] = n;
}
gap[i] = -1;
}
maxgaph = oldh - 1;
}
}
inline void set_height(int x, int newh) {
if (~gapprv[x]) {
if (gapprv[x] == x) {
gapprv[gapnex[x]] = gapnex[x];
gap[h[x]] = gapnex[x];
} else {
gapnex[gapprv[x]] = gapnex[x];
if (~gapnex[x]) {
gapprv[gapnex[x]] = gapprv[x];
}
}
}
if ((h[x] = newh) >= n) {
return;
}
maxgaph = std::max(maxgaph, newh);
if (ov[x]) {
maxh = std::max(maxh, h[x]);
ovnex[x] = ovlist[h[x]];
ovlist[h[x]] = x;
}
if (~(gapnex[x] = gap[h[x]])) {
gapprv[gapnex[x]] = x;
}
gap[h[x]] = gapprv[x] = x;
}
inline void push_flow(int u, edge &e, ANS_TYPE df) {
if (!ov[e.to] && (e.to) != t) {
ovnex[e.to] = ovlist[h[e.to]];
ovlist[h[e.to]] = (e.to);
}
ov[u] -= df, ov[e.to] += df, e.flow -= df, vec[e.to][e.rev].flow += df;
}
inline void relabel() {
workcnt = maxh = maxgaph = 0;
std::fill(h.begin(), h.end(), n), h[t] = 0;
std::fill(gap.begin(), gap.end(), -1);
std::fill(gapprv.begin(), gapprv.end(), -1);
std::fill(gapnex.begin(), gapnex.end(), -1);
std::fill(ovlist.begin(), ovlist.end(), -1);
std::fill(ovnex.begin(), ovnex.end(), -1);
std::fill(cur.begin(), cur.end(), 0);
std::queue<int>q;
q.push(t);
int x;
while (!q.empty()) {
x = q.front(), q.pop();
for (auto &e : vec[x]) {
if (h[e.to] == n && e.to != s && vec[e.to][e.rev].flow) {
set_height(e.to, h[x] + 1);
q.push(e.to);
}
}
}
}
};
HLPP ans;
#define getchar() getchar_unlocked()
//#define getchar() _getchar_nolock()
//可能是因为我本地评测环境奇特,只有 _getchar_nolock()
inline int read() {
int x = 0;
bool f = false;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') {
f = true;
}
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return f ? -x : x;
}
int main() {
int n, m, s, t;
n = read(), m = read();
read();
s = 1, t = n;
ans = HLPP(n, m, s, t);
for (int i = 0; i < ans.m; ++i) {
int u, v, f;
u = read(), v = read(), f = read();
ans.addedge(u, v, f);
}
printf("%d", ans.get_max_flow());
return 0;
}
后记
别问我怎么学的优化,问就是跟这题的榜一学的。
但还是比他慢上不少,我是真看不懂他那奇特的存图方式。
有知道怎么优化的可以私信我。
感谢观看,希望大家能从这篇题解中学到不少东西。