这份 HLPP 代码:
#include <bits/stdc++.h>
#include <bits/extc++.h>
int n, m, s, t, tot = 1, height[1201], gap[3601], head[120001];
long long extra[1201];
std::bitset<1201> used;
struct node {
int x;
node(int x_): x(x_) {}
bool operator<(const node &u)const {return height[x] < height[u.x];}
operator int() const {return x;}
};
std::priority_queue<node, std::vector<node>, std::less<node>> q;
//__gnu_pbds::priority_queue<node, std::less<node>, __gnu_pbds::binary_heap_tag> q;
struct edge {
int to, next;
long long w;
} edge[1200001];
void add(int u, int v, long long w) {
edge[++tot].next = head[u];
edge[tot].to = v;
edge[tot].w = w;
head[u] = tot;
}
void bfs() {
std::memset(height, -1, sizeof(height)), std::memset(gap, 0, sizeof(gap));
std::queue<int> que;
que.push(t), height[t] = 0, gap[0]++;
while (!que.empty()) {
int cur = que.front(); que.pop();
for (int i = head[cur]; i; i = edge[i].next)
if (!edge[i].w && height[edge[i].to] == -1)
height[edge[i].to] = height[cur] + 1, gap[height[edge[i].to]]++, que.push(edge[i].to);
}
}
void relabel(int x) {
gap[height[x]]--;
if (!gap[height[x]])
for (int i = 1; i <= n; i++)
if (height[i] < n + 1 && height[i] > height[x] && i != s && i != t)
gap[height[i]]--, height[i] = n + 1;
height[x] = 0x3f3f3f3f;
for (int i = head[x]; i; i = edge[i].next)
if (edge[i].w && height[x] > height[edge[i].to] + 1)
height[x] = height[edge[i].to] + 1;
gap[height[x]]++;
}
long long HLPP() {
bfs(), height[s] = n;
for (int i = head[s]; i; i = edge[i].next) {
if (edge[i].w)extra[edge[i].to] += edge[i].w, edge[i ^ 1].w += edge[i].w, edge[i].w = 0;
if (edge[i].to != s && edge[i].to != t && !used[edge[i].to])q.push(edge[i].to), used[edge[i].to] = true;
}
while (!q.empty()) {
int cur = q.top(); q.pop(), used[cur] = false;
for (int i = head[cur]; i; i = edge[i].next) {
int v = edge[i].to;
long long w = edge[i].w;
if (height[cur] == height[v] + 1 && w) {
int flow = std::min(w, extra[cur]);
extra[cur] -= flow, extra[v] += flow;
edge[i].w -= flow, edge[i ^ 1].w += flow;
if (v != s && v != t && !used[v])q.push(v), used[v] = true;
}
if (!extra[cur])break;
}
if (extra[cur])relabel(cur), q.push(cur), used[cur] = true;
}
return extra[t];
}
signed main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
std::cin >> n >> m >> s >> t;
for (int i = 1, u, v; i <= m; i++) {long long w; std::cin >> u >> v >> w, add(u, v, w), add(v, u, 0);}
return std::cout << HLPP(), 0;
}
仅仅是加了 39 行的 height[i] < n + 1 ,原来 T 掉的 #5 就过掉了???
为什么优化如此大
顺便问,500ms 的 HLPP 怎么写的,这个优先队列多个 log 的原因吗吸氧 5s