舍弃 Priority_Queue
如果用优先队列维护有超额流的点,会慢很多,我们可以考虑桶写。主要有两个桶,创建如下数组或变量:
Excess_list Excess_nex gap gapnex gappre maxgaph maxh
maxgaph 是全局最高高度,maxh 是有超额流的点中最高高度,推流时按 maxh 将表头取出来用即可。
Excess 开头的两个是存有超额流的点。后面带 list 表示是表头;nex 表示后继。
gap 开头的是存全局所有点。gap 是表头;nex 是后继;pre 是前驱,如果 pre 等于它本身,代表他是表头,pre 同时代表他是否在链表中。
函数 Set_Height 主要是修改点的高度,并同时处理好两个桶用的。
inline void Set_Height(const int u, const int newh) {
if (~gappre[u]) {
//如果在链表中,须移除。
if (gappre[u] == u) {
//是表头。
gap[h[u]] = gapnex[u];
/*
这照理应特判有没有后继
但调用此函数时,
没后继的情况被 gap 剪了,
Globals_Relabel 时又不在链表中。
*/
gappre[gapnex[u]] = gapnex[u];
} else {
gapnex[gappre[u]] = gapnex[u];
//链表基本操作应该都会吧。
if (~gapnex[u]) {
gappre[gapnex[u]] = gappre[u];
}
}
}
if ((h[u] = newh) >= inf) {
return;
}
//高度超过 n 说明这个点没用了,也没必要维护,终止。
maxgaph = std::max(maxgaph, newh);
//更新全局
if (Excess[u]) {
//若还须进行推流,也顺便处理了。
maxh = std::max(maxh, newh);
Excess_nex[u] = Excess_list[newh];
Excess_list[newh] = u;
}
//头插
if (~(gapnex[u] = gap[newh])) {
gappre[gapnex[u]] = u;
}
gap[newh] = gappre[u] = u;
}函数 Push_Flow 也顺便维护了桶。
inline void Push_Flow(const int u, edge &e, const TYPE flow) {
int &v = e.to;
if (!Excess[v] && v != t) {
//对方须加入桶中。
Excess_nex[v] = Excess_list[h[v]];
Excess_list[h[v]] = v;
}
e.flow -= flow, vec[v][e.rev].flow += flow;
Excess[u] -= flow, Excess[v] += flow;
}Globals_Relabel
标签重贴全局只用一次太浪费了,我们完全可以使用多次。
有些超额流一时半会儿推不出去,要反复修改点的高度,而周围的点也推不出超额流,只能点之间反复推流,不断增加点的高度。使用 Globals_Relabel 可以将点的高度一次改好,缩短用时。
实现也很简单,用 workcnt 记录改了几个点的高度,到达 (n<<2) 了就重贴标签,步数清零。
GetMaxFlow() 中的这一段。
if (workcnt >= (n << 2)) {
Globals_Relabel();
}和 Update_Node() 中的这一段。
++workcnt;忽略高度超过 n 的点
这个优化几乎不需要实现就有了。
有些 HLPP 中,会把无法到达汇点的点,高度改为 ,让它们的超额流回到源点。事实上,我们可以完全不管这些点,将他们直接废弃,反而更快。
主要是 Set_Height() 中的这一段。
if ((h[u] = newh) >= inf) {
return;
}
\\无法到达汇点的点,高度没被更新过,也就是 inf。标程
#include <bits/stdc++.h>
using namespace std;
namespace IO {
const int LEN = (1 << 22);
char buf[LEN], *front = buf, *back = buf;
#define GetChar() ((front==back)&&(back=(front=buf)+fread(buf,1,LEN,stdin),front==back)?EOF:(front++)[0])
inline int read() {
int res = 0;
bool flag = false;
char ch = GetChar();
while (ch < '0' || ch > '9') {
if (ch == '-') {
flag = true;
}
ch = GetChar();
}
while (ch >= '0' && ch <= '9') {
res = res * 10 + ch - '0';
ch = GetChar();
}
return flag ? -res : res;
}
}
using namespace IO;
class HLPP {
public:
typedef int TYPE;
static const int maxn = 80005, inf = 0x7f7f7f7f, maxm = inf;
class edge {
public:
int to, rev;
TYPE flow;
edge(int to, TYPE flow, int rev): to(to), flow(flow), rev(rev) {}
edge() {}
};
std::vector<edge>vec[maxn];
TYPE Excess[maxn];
int h[maxn], gap[maxn], gapnex[maxn], gappre[maxn], Excess_list[maxn], Excess_nex[maxn];
int n, m, s, t, maxh, maxgaph, workcnt, cur[maxn];
HLPP(int n, int m, int s, int t): n(n), m(m), s(s), t(t), maxh(0), maxgaph(0), workcnt(0),
Excess({0}), h({0}), gap({-1}), gapnex({-1}), gappre({-1}),
Excess_list({-1}), Excess_nex({-1}), cur({0}) {}
HLPP() {}
inline void Add_Edge(const int from, const int to, const TYPE flow) {
vec[from].push_back(edge(to, flow, vec[to].size()));
vec[to].push_back(edge(from, 0, vec[from].size() - 1));
}
inline void Set_Height(const int u, const int newh) {
if (~gappre[u]) {
if (gappre[u] == u) {
gap[h[u]] = gapnex[u];
gappre[gapnex[u]] = gapnex[u];
} else {
gapnex[gappre[u]] = gapnex[u];
if (~gapnex[u]) {
gappre[gapnex[u]] = gappre[u];
}
}
}
if ((h[u] = newh) >= inf) {
return;
}
maxgaph = std::max(maxgaph, newh);
if (Excess[u]) {
maxh = std::max(maxh, newh);
Excess_nex[u] = Excess_list[newh];
Excess_list[newh] = u;
}
if (~(gapnex[u] = gap[newh])) {
gappre[gapnex[u]] = u;
}
gap[newh] = gappre[u] = u;
}
inline void Globals_Relabel() {
maxh = maxgaph = workcnt = 0;
std::memset(h, inf, sizeof(h)), h[t] = 0;
std::memset(cur, 0, sizeof(cur));
std::memset(gap, -1, sizeof(gap));
std::memset(gapnex, -1, sizeof(gapnex));
std::memset(gappre, -1, sizeof(gappre));
std::memset(Excess_list, -1, sizeof(Excess_list));
std::memset(Excess_nex, -1, sizeof(Excess_nex));
int que[maxn], front = 0, back = -1;
std::memset(que, 0, sizeof(que));
for (que[++back] = t; front <= back;) {
const int u = que[front++];
for (edge &e : vec[u]) {
int &v = e.to;
if (vec[v][e.rev].flow && h[v] == inf && v != s) {
que[++back] = v;
Set_Height(v, h[u] + 1);
}
}
}
}
inline void Push_Flow(const int u, edge &e, const TYPE flow) {
int &v = e.to;
if (!Excess[v] && v != t) {
Excess_nex[v] = Excess_list[h[v]];
Excess_list[h[v]] = v;
}
e.flow -= flow, vec[v][e.rev].flow += flow;
Excess[u] -= flow, Excess[v] += flow;
}
inline void Update_Node(const int u) {
int newh = inf, sz = vec[u].size();
for (int i = cur[u]; i < sz; ++i) {
edge &e = vec[u][i];
if (e.flow) {
int &v = e.to;
if (h[v] + 1 == h[u]) {
Push_Flow(u, e, std::min(Excess[u], e.flow));
if (!Excess[u]) {
cur[u] = i;
return;
}
} else {
newh = std::min(newh, h[v] + 1);
}
}
}
for (int i = 0; i < cur[u]; ++i) {
edge &e = vec[u][i];
if (e.flow) {
newh = std::min(newh, h[e.to] + 1);
}
}
cur[u] = 0, ++workcnt;
const int oldh = h[u];
if (~gapnex[gap[oldh]]) {
Set_Height(u, newh);
} else {
for (int i = oldh; i <= maxgaph; ++i) {
for (int j = gap[i]; ~j; j = gapnex[j]) {
h[j] = inf;
}
gap[i] = -1;
}
maxgaph = oldh - 1;
}
}
inline TYPE GetMaxFlow() {
Globals_Relabel();
for (edge &e : vec[s]) {
if (e.flow && h[e.to] != inf) {
Push_Flow(s, e, e.flow);
maxh = std::max(maxh, h[e.to]);
}
}
for (; maxh; --maxh) {
while (~Excess_list[maxh]) {
int u = Excess_list[maxh];
Excess_list[maxh] = Excess_nex[u];
Update_Node(u);
if (workcnt >= (n << 2)) {
Globals_Relabel();
}
}
}
return Excess[t];
}
inline void Solve() {
int N, M, S, T;
N = read(), M = read(), S = read(), T = read();
this[0] = HLPP(N, M, S, T);
for (int i = 1; i <= m; ++i) {
int u, v, f;
u = read(), v = read(), f = read();
Add_Edge(u, v, f);
}
printf("%d\n", GetMaxFlow());
}
};
HLPP Main;
int main() {
Main.Solve();
return 0;
}