P4722 题解

· · 题解

当年还是蒟蒻的我(现在是奆蒻了)看到这道题的时候,反手一个 ISAP 就拍了上去,结果

于是忍痛学了 HLPP......

先来看看朴素预流推进

在学习增广路算法的时候,我们都是在残量网络中寻找一条或多条最短的增广路,然后沿着这些增广路增广,当无法找到增广路的时候结束算法。如果使用增广路算法中效率较高的 Dinic 或 ISAP 算法,每次最坏找到一条增广路,最多找 n 次,所以最坏时间复杂度是 O(n^2m) 的,而且无论我们怎么改进算法,这个最坏时间复杂度上限都是无法避免的。对于本题 n \le 1200,m \le 120000 的数据来看,O(n^2m) 算法显然死得不能再死了。

于是这道题铁定不能用增广路算法了(如果使用 LCT 或伸缩操作优化当我没说)。

想一想那个用下水管网络比喻网络流的例子?

如果直接模拟“水”的流动会怎么样呢?

我们先假设这样一个情景:

源点有 \infty 个单位的水,然后这些水从源点开始向外面蔓延,塞满了源点向外的所有通道,然后这些通道通向的节点也有了很多的水,这些水继续向外蔓延......一直到汇点,然后我们统计一下汇点有多少单位的水,最大流不就求出来了吗?

看起来很美好,可惜实践中会发生两个节点轮流相互推水,推到 TLE 的情况。

于是我们对每个节点加上一个高度,每次都只向比该节点高度低的节点推水,刚开始源点的高度为 n,然后每次推流都维护这个高度。

于是就能成功运行了!

可惜这样做时间复杂度还是不变的 O(n^2m)

难道就无法改进了吗?

先人的智慧又出现了,每次从高度最高的节点推流,时间复杂度会成为 O(n^2\sqrt m)

可还是 TLE 怎么办?

那就继续优化啊!

类似于 ISAP,我们可以从在刚开始的时候从汇点开始 BFS,对途经节点全部重标号,速度会快很多。

可是依然 TLE......

于是重头戏来了!

gap优化

类似于 ISAP 的 gap 优化,HLPP也有一个 gap 优化,当 0 至 n 之间有深度不存在时,将此深度以上的结点的深度全部设为 n+1,好让这些结点的流推回到源点去。

果然快了许多。

可是,难受T^T

懒惰 gap 优化

我们发现不一定要让深度大于 n 的结点的流退回来,于是我们把这些结点标记为不可到达,然后每次推流和更改高度都不考虑这些结点。

懒惰 gap 优化

终于把这道毒瘤题 A 了!

接下来是代码实现部分

基础部分:

#define inf 1000000000000000
#define V 200010
#define E 1000010
typedef long long int ll;
struct edge {
public:
    int to, next;
    ll capa;
};
int cnt = 0, head[V]; int n, m; edge node[E];
bool vis[V];
inline void add(int fir, int nxt, ll w) {
    node[cnt].to = nxt;
    node[cnt].capa = w;
    node[cnt].next = head[fir];
    head[fir] = cnt; ++cnt;
}
int s, t, dep[V], gap[V], cur2[V]; std::queue<int>que; ll sum = 0; ll cur[V];
bool inque[V];
**BFS 重标号:** ```cpp inline bool bfs() { int fro, ito; que.push(t); dep[t] = 0; ++gap[dep[t]]; while (!que.empty()) { fro = que.front(); que.pop(); for (register int i = head[fro]; i != -1; i = node[i].next) { ito = node[i].to; if (dep[ito] == -1) { dep[ito] = dep[fro] + 1; que.push(ito); ++gap[dep[ito]]; } } } return (dep[s] != -1); } ``` **推流:** 向周围结点拼命灌水。 ```cpp inline void push(int v) { for (int i = cur2[v]; i != -1; i = node[i].next) { cur2[i] = i; if (!cur[v])return; if (dep[v] == dep[node[i].to] + 1 && node[i].capa) { ll ls = std::min(cur[v], node[i].capa); cur[v] -= ls, cur[node[i].to] += ls; node[i].capa -= ls, node[i ^ 1].capa += ls; if (!inque[node[i].to] && node[i].to != s && node[i].to != t && !vis[node[i].to]) { hlpp.push(node[i].to); inque[node[i].to] = true; } } } } ``` **重标号:** 其实就是把当前结点的高度改为附近高度最低的结点的高度 + 1。 ```cpp inline void relable(int v) { dep[v] = n + 5; for (register int i = head[v]; i != -1; i = node[i].next) { if (node[i].capa && dep[v] > dep[node[i].to]) dep[v] = dep[node[i].to]; } ++dep[v]; } ``` **主程序:** 有个问题:怎么才能取出高度最高的结点? 用 ```std::priority_queue``` 维护即可。 ```cpp inline ll HLPP() { cur[s] = inf; initing(); if (!bfs()) return 0; int np; int v; ll w; ll ans = 0; dep[s] = n; for (int i = head[s]; i != -1; i = node[i].next) { v = node[i].to; w = node[i].capa; cur[s] -= w; cur[v] += w; node[i].capa -= w; node[i ^ 1].capa += w; if (v != s && v != t && !vis[v]) { hlpp.push(v); inque[v] = true; } } while (!hlpp.empty()) { memcpy(cur2, head, (n + 1) * sizeof(int)); np = hlpp.top(); hlpp.pop(); inque[np] = false; push(np); if (cur[np]) { if (--gap[dep[np]] == 0) { for (int i = 1; i <= n; i++) if (i != s && i != t && dep[i] > dep[np] && dep[i] < n + 1) dep[i]=n+1, vis[i] = true; } relable(np); ++gap[dep[np]]; hlpp.push(np); inque[np] = true; } } return cur[t]; } ``` **AC 代码:** ```cpp #include <bits/stdc++.h> #define inf 1000000000000000 #define V 200010 #define E 1000010 typedef long long int ll; struct edge { public: int to, next; ll capa; }; int cnt = 0, head[V]; int n, m; edge node[E]; bool vis[V]; inline void add(int fir, int nxt, ll w) { node[cnt].to = nxt; node[cnt].capa = w; node[cnt].next = head[fir]; head[fir] = cnt; ++cnt; } int s, t, dep[V], gap[V], cur2[V]; std::queue<int>que; ll sum = 0; ll cur[V]; bool inque[V]; inline void initing() { memset(dep, -1, V * sizeof(int)); } inline bool bfs() { int fro, ito; que.push(t); dep[t] = 0; ++gap[dep[t]]; while (!que.empty()) { fro = que.front(); que.pop(); for (register int i = head[fro]; i != -1; i = node[i].next) { ito = node[i].to; if (dep[ito] == -1) { dep[ito] = dep[fro] + 1; que.push(ito); ++gap[dep[ito]]; } } } return (dep[s] != -1); } struct cmp { public: inline bool operator()(int a, int b) { return dep[a] < dep[b]; } }; std::priority_queue<int, std::vector<int>, cmp>hlpp; inline void push(int v) { for (int i = cur2[v]; i != -1; i = node[i].next) { cur2[i] = i; if (!cur[v])return; if (dep[v] == dep[node[i].to] + 1 && node[i].capa) { ll ls = std::min(cur[v], node[i].capa); cur[v] -= ls, cur[node[i].to] += ls; node[i].capa -= ls, node[i ^ 1].capa += ls; if (!inque[node[i].to] && node[i].to != s && node[i].to != t && !vis[node[i].to]) { hlpp.push(node[i].to); inque[node[i].to] = true; } } } } inline void relable(int v) { dep[v] = n + 5; for (register int i = head[v]; i != -1; i = node[i].next) { if (node[i].capa && dep[v] > dep[node[i].to]) dep[v] = dep[node[i].to]; } ++dep[v]; } inline ll HLPP() { cur[s] = inf; initing(); if (!bfs()) return 0; int np; int v; ll w; ll ans = 0; dep[s] = n; for (int i = head[s]; i != -1; i = node[i].next) { v = node[i].to; w = node[i].capa; cur[s] -= w; cur[v] += w; node[i].capa -= w; node[i ^ 1].capa += w; if (v != s && v != t && !vis[v]) { hlpp.push(v); inque[v] = true; } } while (!hlpp.empty()) { memcpy(cur2, head, (n + 1) * sizeof(int)); np = hlpp.top(); hlpp.pop(); inque[np] = false; push(np); if (cur[np]) { if (--gap[dep[np]] == 0) { for (int i = 1; i <= n; i++) if (i != s && i != t && dep[i] > dep[np] && dep[i] < n + 1) dep[i]=n+1, vis[i] = true; } relable(np); ++gap[dep[np]]; hlpp.push(np); inque[np] = true; } } return cur[t]; } int main() { std::ios::sync_with_stdio(0); std::cin.tie(); std::cout.tie(); memset(head, -1, V * sizeof(int)); std::cin >> n >> m >> s >> t; int f, l; ll w; for (register int i = 0; i < m; i++) { std::cin >> f >> l >> w; add(f, l, w); add(l, f, 0); } std::cout << HLPP(); return 0; } ```