1. 基本定义
一个网络(network)是一个有向图 ,每条边 ,有容量(capacity) 和流量(flow)。 时 。
可行流包括有源汇和无源汇。有源汇有源点和汇点, 是源点(source), 是汇点(sink)。
对于一个可行流 ,其满足
- 容量限制:
- 斜对称性:
- 流守恒性:
根据流守恒性,源点流出的流量总是等于流入汇点的流量,将其记为 ,即 的流量。
定义 在 上的残量网络(Residual Network) 是容量函数为 的网络。 等价于边 在 上不存在。
容量限制等价于残量网络的容量非负。
注意
残量网路可以存在原图上没有的边 ,即 。此时要求其反边流量 ,所以 ,那么 ,即边 存在。
关于重边、反边和自环
这几种情况都可以通过增加额外的点建模为等价的没有这几种情况的网络,并且上述定义也可以改为定义在边上以独立处理这几种情况,因此无需特别判定。
2. 网络最大流
对一个网络流,求其可行流 最大的 ,这就是最大流问题。
2.1 Push-Relabel 预流推进算法
此算法采用的暂时忽略流守恒性,将流存储在节点中接着再流出去的思想更加符合流这一概念的直觉印象,因此先讲解该算法。
定义:
流守恒性即等价于 ,此时亦可以推出 。
在此假设 。
WARNING
接下来可能会出现 状物,其意义与上文 相同, 同理。
对于 定义推流操作 为将边 尽可能流满并同时维护其反向边。
伪代码为:
对于 定义重贴标签操作 操作为调整 。
伪代码为:
此时容易得出暴力算法为一开始从 输出满流,再任意进行上述两个操作,直到没有超额流,最后 即为最大的 。
注意到暴力算法很玄学,因此考虑优化。(具体的正确性和时间复杂度证明可见参考资料部分中引用的论文)
最高标号预流推进算法(Highest Label Preflow Push)在上述通用的预流推送算法中,在每次选择结点时,都优先选择高度最高的溢出结点(即 )并对它所有可以推送的边进行推送,如果仍溢出,对它重贴标签;算法复杂度为 。
注意到 函数很像最短路,因此一开始可以从反边跑到 的最短路,并将所有非 的 初始化为其到 的最短路以此进行优化。
为什么不将 也初始化为其最短路?
设想这样一种情况:存在边 ,那么此时 ,一开始从 推出满流后周围的点就会试图 回 。要解决这种情况必须在推流后 ,并加入一些修改,但这样可能会使时间复杂度不正确。
所以如果发现了将 也初始化为其最短路还能保证正确性、时间复杂度的实现方法,请务必通知我。
感性理解:
可以将 函数理解为高度,初始点 高度最高,而随着流不断往下,高度也不断减小,而一个点一旦高度过低就提升其高度让其中的残余流量能流出去。但 时就说明无法再流出去,此时只能流回 ,即等同于将流撤销。
从这个角度来说,其本质上还是反悔贪心。
抓智谱清言过来写了个实现,码风还算优良:
AC
以下代码使用 AI 辅助生成(但码风还挺优良的说是)。
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using ll = long long;
const ll maxn = 1200;
// 注意:如果 maxn 很大(如 1e5),建议 B 数组开在 main 函数里或设为 static,避免栈溢出
const ll maxm = 120000;
struct flow
{
template <class t>
t &qvmax(t &a, const t &b)
{
if (a < b)
return a = b;
return a;
}
template <class t>
t &qvmin(t &a, const t &b)
{
if (b < a)
return a = b;
return a;
}
static const int verybig_int = 0x3f3f3f3f;
static const int verybig_byte = 0x3f;
struct bian
{
int v;
ll limit;
std::size_t fan_bian;
};
std::vector<bian> G[maxn + 1];
void add_edge(int u, int v, ll w)
{
G[u].emplace_back(bian{v, w, G[v].size()});
G[v].emplace_back(bian{u, 0, G[u].size() - 1});
}
int s, t, n;
// 高度和 Gap 数组大小建议稍微开大一点,防止 n+1 越界
// 也可以直接开 maxn + 5
int h[maxn + 5], gap[maxn + 5];
ll extra_flow[maxn + 5];
// 【修改点 1】使用 vector 代替 stack,空间利用率更高,且只需增加少量空间即可支持 n+1 高度
std::vector<int> B[maxn + 5];
int level;
bool get_h()
{
std::memset(h, verybig_byte, sizeof h);
std::queue<int> q;
h[t] = 0;
q.push(t);
while (!q.empty())
{
int u = q.front();
q.pop();
for (const bian &b : G[u])
{
const bian &p = G[b.v][b.fan_bian];
// 反向 BFS 寻找增广路
if (p.limit != 0 && h[b.v] > h[u] + 1)
{
h[b.v] = h[u] + 1;
q.push(b.v);
}
}
}
return h[s] != verybig_int;
}
bool push(int u)
{
for (bian &b : G[u])
{
bian &p = G[b.v][b.fan_bian];
if (b.limit == 0 || (u != s && h[u] != h[b.v] + 1) || h[b.v] == verybig_int)
continue;
ll k = (u == s) ? b.limit : std::min(b.limit, extra_flow[u]);
// 【修改点 2】vector 模拟栈操作:push_back
if (b.v != s && b.v != t && extra_flow[b.v] == 0)
{
B[h[b.v]].push_back(b.v);
qvmax(level, h[b.v]);
}
extra_flow[u] -= k;
extra_flow[b.v] += k;
b.limit -= k;
p.limit += k;
if (extra_flow[u] == 0)
return 0;
}
return extra_flow[u] != 0;
}
void relabel(int u)
{
h[u] = verybig_int;
for (bian &b : G[u])
if (b.limit != 0)
qvmin(h[u], h[b.v]);
++h[u];
// 【修改点 3】允许高度达到 n+1
// 只有高度小于 n+2 (即 <= n+1) 才放入桶中
// 这样可以把流推回源点 s (h[s] = n)
if (h[u] < n + 2)
{
B[h[u]].push_back(u);
qvmax(level, h[u]);
++gap[h[u]];
}
}
int select()
{
while (level >= 0 && B[level].empty())
level--;
// 【修改点 4】vector 模拟栈操作:back()
return (level >= 0 ? B[level].back() : 0);
}
ll maxflow(int s_, int t_, int n_)
{
level = 0;
s = s_;
t = t_;
n = n_;
// 【修改点 5】必须初始化 extra_flow 为 0
std::memset(extra_flow, 0, sizeof extra_flow);
if (!get_h())
return 0;
// gap 数组初始化范围根据 h 的最大值调整,n+2 足够
std::memset(gap, 0, sizeof(int) * (n + 5));
for (int i = 1; i <= n; i++)
if (h[i] != verybig_int)
gap[h[i]]++;
h[s] = n;
push(s);
for (int u; (u = select()) != 0;)
{
// 【修改点 6】vector 模拟栈操作:pop_back
B[level].pop_back();
if (push(u))
{
if (--gap[h[u]] == 0)
{
// Gap 优化:当前高度断层,将所有高于此高度的节点高度设为 n+1
// 限制循环范围优化性能,也可以遍历 1..n
for (int i = 1; i <= n; i++)
if (i != s && h[i] > h[u] && h[i] <= n + 1)
h[i] = n + 1;
}
relabel(u);
}
}
// 此时 extra_flow[s] + extra_flow[t] 应该为 0
return extra_flow[t];
}
} g;
namespace fast_IO
{
constexpr uint32_t IOSIZE = 100000;
uint32_t precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; // ����ȡ����С������ precision λ
char obuf[IOSIZE], *p3 = obuf;
#if defined(ONLINE_JUDGE) or defined(FastIO)
char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;
#define getchar() ((p1 == p2) and (p2 = (p1 = ibuf) + fread(ibuf, 1, IOSIZE, stdin), p1 == p2) ? (EOF) : (*p1++))
#define putchar(x) ((p3 == obuf + IOSIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf), *p3++ = x)
#endif
template <typename t>
inline bool read(t &s)
{
s = 0;
int w = 1;
char ch;
while (ch = getchar(), !(ch > 47 and ch < 58) and (ch != EOF))
if (ch == '-')
w = -1;
if (ch == EOF)
return false;
while ((ch > 47 and ch < 58))
s = s * 10 + (ch - 48), ch = getchar();
return s *= w, true;
}
template <typename t>
inline void print(t x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + 48);
}
inline bool read(char &s)
{
while (s = getchar(), isspace(s) and s != EOF)
;
return s != EOF;
}
inline void print(char x) { putchar(x); }
inline bool read(char *s)
{
char ch;
while (ch = getchar(), (ch < 33) && ch != EOF)
;
if (ch == EOF)
return false;
while (!(ch < 33) and (ch != EOF))
*s++ = ch, ch = getchar();
*s = (char)0;
return true;
}
inline void print(char *x)
{
while (*x)
putchar(*x++);
}
inline void print(const char *x)
{
for (int i = 0; x[i]; i++)
putchar(x[i]);
}
inline bool read(std::string &s)
{
s = "";
char ch;
while (ch = getchar(), (ch < 33) and ch != EOF)
;
if (ch == EOF)
return false;
while (!(ch < 33))
s += ch, ch = getchar();
return true;
}
inline void print(std::string x)
{
for (int i = 0, n = x.size(); i < n; i++)
putchar(x[i]);
}
inline bool read(bool &b)
{
char ch;
while (ch = getchar(), (ch < 33) and ch != EOF)
;
return ch == EOF ? false : (b = ch ^ 48, true);
}
inline void print(bool b) { putchar(b + 48); }
inline bool read(double &x)
{
char ch = getchar();
int f = 1;
for (x = 0; (ch < 48 or 57 < ch) and (ch != EOF); ch = getchar())
if (ch == '-')
f = -1;
if (ch == EOF)
return false;
for (x = 0; 47 < ch and ch < 58; ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch != '.')
return x *= f, true;
double y = 0.1;
for (ch = getchar(); 47 < ch and ch < 58; ch = getchar())
x += y * (ch ^ 48), y /= 10;
return x *= f, true;
}
inline void print(double x)
{
if (x < 0)
putchar('-'), x = -x;
if (!precision)
print((unsigned long long)(x - (unsigned long long)(x) >= 0.5 ? x + 1 : x));
else
{
unsigned long long xx = x;
double y = ((x - xx) * POW[precision]);
unsigned long long yy = (y - (unsigned long long)(y) >= 0.5 ? y + 1 : y);
if (yy == POW[precision])
xx++, yy = 0;
print(xx), putchar('.');
for (int j = precision - 1; ~j; j--)
putchar(48 + yy / POW[j] % 10);
}
}
template <typename t, typename... T1>
inline int read(t &a, T1 &...other) { return read(a) + read(other...); }
template <typename t, typename... T1>
inline void print(t a, T1... other) { print(a), print(other...); }
template <typename t>
inline t read()
{
t a;
return read(a), a;
}
struct Fast_IO
{
bool flag = 1;
inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); }
inline void SetPrecision(int x)
{
if (x > 9)
throw std::runtime_error("Precision too high!");
else if (x < 0)
throw std::runtime_error("Precision too low!");
else
precision = x;
} // ������������Ϊ x������ȷ��С����� x λ��
inline operator bool() { return flag; }
} io;
Fast_IO &operator>>(Fast_IO &io, char *b) { return io.flag &= read(b), io; }
template <typename t>
Fast_IO &operator>>(Fast_IO &io, t &b) { return io.flag &= read(b), io; }
template <typename t>
Fast_IO &operator<<(Fast_IO &io, t b) { return print(b), io; }
constexpr char endl = '\n';
}
using fast_IO::io;
int n, m, s, t;
int main()
{
#ifdef fre_io
freopen("in.in", "r", stdin);
freopen("in.out", "w", stdout);
#endif
io >> n >> m >> s >> t;
for (int i = 1; i <= m; i++)
{
int u, v;
ll w;
io >> u >> v >> w;
g.add_edge(u, v, w);
}
io << g.maxflow(s, t, n) << fast_IO::endl;
return 0;
}快读快写使用了来自 @liujiaxi123456 的模板。
2.2 最小割
定义网络 的割(cut)为 两个集合,其中 ,。一个割的容量 。最小割就是最小的割。
最大流最小割定理
即最大流总是等于最小割。
这里只给出大概理解,严谨证明见文后附的参考资料部分。
对于任意可行流 和最小割 ,,且必然存在一个最大流 取等。如果该流小于最小割,那么残量网络上 和 必然仍然连通;假设存在一些边正好为 使得其不连通,那么其必然小于 ,这样却又可以构造出一个更小的割,因此矛盾。该流大于最小割就意味着残量网络存在负数,即 不为可行流,矛盾。
2.3 增广
增广路,即残量网络 上 的一条路径。
那么容易想到一项可能的算法:每次寻找一个增广路 ,然后找到 上的最小割,根据最小割最大流定理,该最小割即为 上的最大流。此类算法的时间复杂度上界为 。
贪心如何保证正确性?网络流的斜对称性即保证了其正确性。反边 增加 的容量即等价于 减少 的容量。
当网络流遇见老夫子
2.4 Dinic 算法
Dinic 算法实际上蕴含了 Edmonds-Karp 算法,因此本文直接讲解 Dinic 算法。
Dinic 算法主要流程为:
- 从 开始 BFS 找残量网络上的最短路,也即求层次图 。
- 在 上 DFS 出阻塞流 。
- 将 并入到 中。
- 若残量网络仍存在从 到 的一条增广路,则重复。
在一次从 开始的 DFS 中,可能多次增广到 ,并同时尝试从 开始 DFS。当前弧优化的原理则是:若 已被增广过一次,跳过其所有已在后侧被阻塞的边 。
当前弧优化确保了 Dinic 算法复杂度的上界为 。2
存在一种做法使得其复杂度为 ,其中 为容量值域。但用途较为局限,因此本文在此不做介绍。有兴趣的读者可以翻阅下方参考资料部分。
AC
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstring>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
using ll = long long;
template <class T>
T &qvmin(T &a, const T &b)
{
if (b < a)
return a = b;
return a;
}
template <class T>
T &qvmax(T &a, const T &b)
{
if (a < b)
return a = b;
return a;
}
const ll maxn = 200;
const ll maxm = 5000;
int n, m, s, t;
struct flow
{
vector<int> G[maxn + 1];
struct bian
{
int u, v;
ll limit;
//残量网络
};
vector<bian> E;
std::size_t push_new(bian obj)
{
E.push_back(obj);
swap(obj.u, obj.v);
obj.limit = 0;
E.push_back(obj);
//对应的反边顺便建了
return E.size() - 2;
}
void add_edge(int u, int v, ll w)
{
auto zzz = push_new(bian{u, v, w});
G[u].push_back(zzz);
G[v].push_back(zzz ^ 1);
}
int T;
int cur[maxn + 1], dis[maxn + 1];
ll dfs(int u, ll res)
{
if (u == T)
return res;
ll flow = 0;
for (int i = cur[u]; i < G[u].size() && res > 0; i++)
{
const int &E_index = G[u][i];
const int &v = E[E_index].v;
cur[u] = i;
//探过下次就不用再探
const ll min_c = min(res, E[E_index].limit);
//残量网络上必然非负,否则不满足网络流条件
if(min_c < 0)
throw std::invalid_argument("Negative Residual Capacity,Something wrong.");
if (dis[u] + 1 == dis[v] && min_c != 0)//分层图 + 判断是否有剩余流量
{
const ll k = dfs(v, min_c);
flow += k;//已统计流量增加
res -= k;//能分出去的流量减
//对应的残量网络
E[E_index].limit -= k;
//残量网络 -= 流量
E[E_index ^ 1].limit += k;
//反边要满足斜对称性
}
}
if (flow == 0)
dis[u] = -1;
//悲报:不存在增广路
return flow;
}
ll maxflow(int s, int t)
{
T = t;
ll flow = 0;
while (1)
{
queue<int> q;
memset(cur, 0, sizeof cur);
memset(dis, -1, sizeof dis);
q.push(s);
dis[s] = 0;
while (!q.empty())
{
int zzz = q.front();
q.pop();
for (int i = 0; i < G[zzz].size(); i++)
{
if (dis[E[G[zzz][i]].v] == -1 && E[G[zzz][i]].limit != 0)
{
dis[E[G[zzz][i]].v] = dis[zzz] + 1;
q.push(E[G[zzz][i]].v);
}
//在存在的残量网络上枚举路径,边权为 0 时残量边不存在
}
}
if (dis[t] == -1)
return flow;
flow += dfs(s, 1e18);
}
}
} g;
int main()
{
#ifdef fre_io
freopen("in.in", "r", stdin);
freopen("in.out", "w", stdout);
#endif
cin >> n >> m >> s >> t;
for (int i = 1; i <= m; i++)
{
int u, v;
ll w;
cin >> u >> v >> w;
g.add_edge(u, v, w);
}
cout << g.maxflow(s, t) << endl;
return 0;
}3. 上下界网络流
即为流 添加了下界,要求 ,也即残量网络 ,其中 。
3.1 无源汇上下界可行流
考虑设置超级源点 与超级汇点 ,将其建模为最大流问题。
不妨假设每条边已经存在 的流量,即 。但此时流守恒性可能会不满足,所以借用 2.1 Push-Relabel 预流推进算法中的超额流概念(此时可能为负)。
对于 , 时,无需特别操作; 时新建边 ; 时新建边 。
从 到 跑最大流,如果附加边满流,则说明这一个点的流量平衡条件可以满足,否则这个点的流量平衡条件不满足。
最后检验 出去的流是否全部满流即可。因为 ,所以这也等价于进入 的边全部满流。
为什么是从 连到入流过多,出流过多则到 ?
因为实际上新建边是在残量网络的意义下建边,而 , 就等价于 ,。
因此 和 在流量上的意义则是消耗多余的流、补足缺少的流以保证流守恒性,也可以说其模拟了多余或缺少了的那一部分流量。
从这个意义上来说, 反而是汇点, 反而是源点。只有在残量网络 上 能满流(最大流满流)时,原网络 上才存在一个可行流。
AC
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using ll = long long;
const ll maxn = 1e3 + 3;
const ll maxm = 1e4;
class network_maxflow
{
protected:
template <class T>
T &qvmax(T &a, const T &b) { return ((a < b) ? (a = b) : a); }
template <class T>
T &qvmin(T &a, const T &b) { return ((b < a) ? (a = b) : a); }
static constexpr int verybig_int = 0x3f3f3f3f, verybig_byte = 0x3f;
static constexpr ll verybig_ll = 0x3f3f3f3f3f3f3f3f;
typedef unsigned pt_type;
struct bian
{
pt_type v;
ll limit; // 残量网络 费用
std::size_t fan_bian;
unsigned id;
};
std::vector<std::vector<bian>> G;
pt_type s, t, n;
private:
std::vector<bool> vis;
std::vector<std::size_t> cur;
std::vector<ll> dis;
public:
network_maxflow()
{
vis.resize(maxn + 1);
cur.resize(maxn + 1);
dis.resize(maxn + 1);
G.resize(maxn + 1);
}
void add_edge(pt_type u, pt_type v, ll limit_of_edge, unsigned id)
{
G[u].emplace_back(bian{v, limit_of_edge, G[v].size(), id});
G[v].emplace_back(bian{u, 0, G[u].size() - 1, 0});
}
bool bellman_ford()
{
std::queue<pt_type> q;
std::fill(vis.begin(), vis.end(), 0);
std::fill(dis.begin(), dis.end(), verybig_ll);
q.push(s);
dis[s] = 0;
vis[s] = 1;
while (!q.empty())
{
pt_type u = q.front();
q.pop();
vis[u] = 0;
for (const bian &b : G[u])
{
if (b.limit != 0 && dis[b.v] > dis[u] + 1)
{
dis[b.v] = dis[u] + 1;
if (!vis[b.v])
{
q.push(b.v);
vis[b.v] = 1;
}
}
}
}
std::fill(cur.begin(), cur.end(), 0);
return (dis[t] != verybig_ll);
} // used SPFA
ll dfs(pt_type u, ll lim)
{
if (u == t)
return lim;
ll ret = 0;
vis[u] = 1;
for (std::size_t i = cur[u]; i < G[u].size(); i++)
{
cur[u] = i;
bian &b = G[u][i];
bian &p = G[b.v][b.fan_bian];
if (!vis[b.v] && b.limit != 0 && dis[b.v] == dis[u] + 1)
{
ll k = dfs(b.v, std::min(lim, b.limit));
if (k == 0)
dis[b.v] = verybig_int;
b.limit -= k;
p.limit += k;
ret += k;
lim -= k;
if (lim == 0)
break;
}
}
vis[u] = 0;
return ret;
}
ll maxflow(pt_type s_, pt_type t_, pt_type n_)
{
s = s_, t = t_, n = n_;
ll flow = 0;
std::fill(vis.begin(), vis.end(), 0);
while (bellman_ford())
{
flow += dfs(s, verybig_int);
// std::cout << flow << std::endl;
}
return flow;
} // Dinic
};
class network_ableflow : protected network_maxflow
{
std::vector<ll> exf;
std::vector<ll> ret;
public:
network_ableflow()
{
exf.resize(maxn + 1);
ret.resize(maxm + 1);
}
void add_edge(pt_type u, pt_type v, ll b, ll c, unsigned id)
{
network_maxflow::add_edge(u, v, c - b, id);
exf[u] -= b;
exf[v] += b;
ret[id] += b;
}
std::vector<ll> solve(pt_type n)
{
pt_type s = n + 1, t = n + 2;
ll flow_s = 0, flow_t = 0;
for (pt_type i = 1; i <= n; i++)
{
if (exf[i] > 0)
{
network_maxflow::add_edge(s, i, exf[i], 0);
flow_s += exf[i];
}
else if (exf[i] < 0)
{
network_maxflow::add_edge(i, t, -exf[i], 0);
flow_t -= exf[i];
}
exf[i] = 0;
}
if (flow_s != flow_t)
throw std::runtime_error("Something wrong.");
auto mx_flow = network_maxflow::maxflow(s, t, n);
if (mx_flow != flow_s)
throw std::invalid_argument("Unable to find.");
for (pt_type i = 1; i <= n; i++)
{
for (const auto &e : G[i])
{
const bian &p = G[e.v][e.fan_bian];
if (e.id == 0)
continue;
ret[e.id] += p.limit;
}
}
return ret;
}
} g;
int n, m, s, t;
int main()
{
#ifdef fre_io
freopen("in.in", "r", stdin);
freopen("in.out", "w", stdout);
#endif
std::cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
ll l, r;
std::cin >> u >> v >> l >> r;
g.add_edge(u, v, l, r, i);
}
try
{
std::vector<ll> zzz;
zzz = g.solve(n);
std::cout << "Yes" << std::endl;
for (int i = 1; i <= m; i++)
std::cout << zzz[i] << std::endl;
}
catch (const std::invalid_argument &obj)
{
std::cout << "No" << std::endl;
}
return 0;
}继承,可是 OOP 之征啊!
3.2 有源汇上下界可行流
连接边 即可转化为无源汇上下界可行流。
3.3 有源汇上下界最大流
找到一个上下界可行流之后再在其残量网路基础上跑 最大流即可。
无需,也不应删除边 ,因为需要计入流量 。
AC
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using ll = long long;
const ll maxn = 1e3 + 3;
const ll maxm = 1e4;
class network_maxflow
{
protected:
template <class T>
T &qvmax(T &a, const T &b) { return ((a < b) ? (a = b) : a); }
template <class T>
T &qvmin(T &a, const T &b) { return ((b < a) ? (a = b) : a); }
static constexpr int verybig_int = 0x3f3f3f3f, verybig_byte = 0x3f;
static constexpr ll verybig_ll = 0x3f3f3f3f3f3f3f3f;
typedef unsigned pt_type;
struct bian
{
pt_type v;
ll limit; // 残量网络 费用
std::size_t fan_bian;
unsigned id;
};
std::vector<std::vector<bian>> G;
pt_type s, t, n;
private:
std::vector<bool> vis;
std::vector<std::size_t> cur;
std::vector<ll> dis;
public:
network_maxflow()
{
vis.resize(maxn + 1);
cur.resize(maxn + 1);
dis.resize(maxn + 1);
G.resize(maxn + 1);
}
void add_edge(pt_type u, pt_type v, ll limit_of_edge, unsigned id)
{
G[u].emplace_back(bian{v, limit_of_edge, G[v].size(), id});
G[v].emplace_back(bian{u, 0, G[u].size() - 1, 0});
}
bool bellman_ford()
{
std::queue<pt_type> q;
std::fill(vis.begin(), vis.end(), 0);
std::fill(dis.begin(), dis.end(), verybig_ll);
q.push(s);
dis[s] = 0;
vis[s] = 1;
while (!q.empty())
{
pt_type u = q.front();
q.pop();
vis[u] = 0;
for (const bian &b : G[u])
{
if (b.limit != 0 && dis[b.v] > dis[u] + 1)
{
dis[b.v] = dis[u] + 1;
if (!vis[b.v])
{
q.push(b.v);
vis[b.v] = 1;
}
}
}
}
std::fill(cur.begin(), cur.end(), 0);
return (dis[t] != verybig_ll);
} // used BFS
ll dfs(pt_type u, ll lim)
{
if (u == t)
return lim;
ll ret = 0;
vis[u] = 1;
for (std::size_t i = cur[u]; i < G[u].size(); i++)
{
cur[u] = i;
bian &b = G[u][i];
bian &p = G[b.v][b.fan_bian];
if (!vis[b.v] && b.limit != 0 && dis[b.v] == dis[u] + 1)
{
ll k = dfs(b.v, std::min(lim, b.limit));
if (k == 0)
dis[b.v] = verybig_int;
b.limit -= k;
p.limit += k;
ret += k;
lim -= k;
if (lim == 0)
break;
}
}
vis[u] = 0;
return ret;
}
virtual ll maxflow(pt_type s_, pt_type t_, pt_type n_)
{
s = s_, t = t_, n = n_;
ll flow = 0;
std::fill(vis.begin(), vis.end(), 0);
while (bellman_ford())
{
flow += dfs(s, verybig_int);
// std::cout << flow << std::endl;
}
return flow;
} // Dinic
};
class network_ableflow : protected network_maxflow
{
protected:
std::vector<ll> exf;
std::vector<ll> ret;
public:
network_ableflow()
{
exf.resize(maxn + 1);
ret.resize(maxm + 1);
}
void add_edge(pt_type u, pt_type v, ll b, ll c, unsigned id)
{
network_maxflow::add_edge(u, v, c - b, id);
exf[u] -= b;
exf[v] += b;
ret[id] += b;
}
std::vector<ll> solve(pt_type n)
{
pt_type s = n + 1, t = n + 2;
ll flow_s = 0, flow_t = 0;
for (pt_type i = 1; i <= n; i++)
{
if (exf[i] > 0)
{
network_maxflow::add_edge(s, i, exf[i], 0);
flow_s += exf[i];
}
else if (exf[i] < 0)
{
network_maxflow::add_edge(i, t, -exf[i], 0);
flow_t -= exf[i];
}
exf[i] = 0;
}
if (flow_s != flow_t)
throw std::runtime_error("Something wrong.");
auto mx_flow = network_maxflow::maxflow(s, t, n + 2);
// std::cerr << mx_flow << ' ' << flow_s << std::endl;
if (mx_flow != flow_s)
throw std::invalid_argument("N");
for (pt_type i = 1; i <= n; i++)
{
for (const auto &e : G[i])
{
const bian &p = G[e.v][e.fan_bian];
if (e.id == 0)
continue;
ret[e.id] += p.limit;
}
}
for (pt_type i = 1; i <= n; i++)
{
while (!G[i].empty() && (G[i].rbegin()->v == s || G[i].rbegin()->v == t))
G[i].pop_back();
}
G[s].clear();
G[t].clear();
return ret;
}
};
class network_biggestflow : public network_ableflow
{
public:
ll maxflow(pt_type s_, pt_type t_, pt_type n_) override
{
pt_type s = s_, t = t_, n = n_;
add_edge(t, s, 0, verybig_ll, 0);
solve(n);
// std::cerr << G[s].rbegin()->limit << std::endl;
G[t].rbegin()->limit = 0;
return network_ableflow::maxflow(s, t, n);
}
} g;
int n, m, s, t;
int main()
{
#ifdef fre_io
freopen("in.in", "r", stdin);
freopen("in.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> n >> m >> s >> t;
for (int i = 1; i <= m; i++)
{
int u, v;
ll l, r;
std::cin >> u >> v >> l >> r;
g.add_edge(u, v, l, r, i);
}
try
{
auto zzz = g.maxflow(s, t, n);
std::cout << zzz << std::endl;
}
catch (const std::invalid_argument &obj)
{
std::cout << obj.what() << std::endl;
}
return 0;
}依旧面向对象。
3.4 有源汇上下界最小流
将 设为 ,即在残量网络 上去掉 后求 的最大流即可退掉多余的流。
最后答案为 。
AC
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using ll = long long;
const ll maxn = 1e3 + 3;
const ll maxm = 1e4;
class network_maxflow
{
protected:
template <class T>
T &qvmax(T &a, const T &b) { return ((a < b) ? (a = b) : a); }
template <class T>
T &qvmin(T &a, const T &b) { return ((b < a) ? (a = b) : a); }
static constexpr int verybig_int = 0x3f3f3f3f, verybig_byte = 0x3f;
static constexpr ll verybig_ll = 0x3f3f3f3f3f3f3f3f;
typedef unsigned pt_type;
struct bian
{
pt_type v;
ll limit; // 残量网络 费用
std::size_t fan_bian;
unsigned id;
};
std::vector<std::vector<bian>> G;
pt_type s, t, n;
private:
std::vector<bool> vis;
std::vector<std::size_t> cur;
std::vector<ll> dis;
public:
network_maxflow()
{
vis.resize(maxn + 1);
cur.resize(maxn + 1);
dis.resize(maxn + 1);
G.resize(maxn + 1);
}
void add_edge(pt_type u, pt_type v, ll limit_of_edge, unsigned id)
{
G[u].emplace_back(bian{v, limit_of_edge, G[v].size(), id});
G[v].emplace_back(bian{u, 0, G[u].size() - 1, 0});
}
bool bellman_ford()
{
std::queue<pt_type> q;
std::fill(vis.begin(), vis.end(), 0);
std::fill(dis.begin(), dis.end(), verybig_ll);
q.push(s);
dis[s] = 0;
vis[s] = 1;
while (!q.empty())
{
pt_type u = q.front();
q.pop();
vis[u] = 0;
for (const bian &b : G[u])
{
if (b.limit != 0 && dis[b.v] > dis[u] + 1)
{
dis[b.v] = dis[u] + 1;
if (!vis[b.v])
{
q.push(b.v);
vis[b.v] = 1;
}
}
}
}
std::fill(cur.begin(), cur.end(), 0);
return (dis[t] != verybig_ll);
} // used BFS
ll dfs(pt_type u, ll lim)
{
if (u == t)
return lim;
ll ret = 0;
vis[u] = 1;
for (std::size_t i = cur[u]; i < G[u].size(); i++)
{
cur[u] = i;
bian &b = G[u][i];
bian &p = G[b.v][b.fan_bian];
if (!vis[b.v] && b.limit != 0 && dis[b.v] == dis[u] + 1)
{
ll k = dfs(b.v, std::min(lim, b.limit));
if (k == 0)
dis[b.v] = verybig_int;
b.limit -= k;
p.limit += k;
ret += k;
lim -= k;
if (lim == 0)
break;
}
}
vis[u] = 0;
return ret;
}
virtual ll maxflow(pt_type s_, pt_type t_, pt_type n_)
{
s = s_, t = t_, n = n_;
ll flow = 0;
std::fill(vis.begin(), vis.end(), 0);
while (bellman_ford())
{
flow += dfs(s, verybig_int);
// std::cout << flow << std::endl;
}
return flow;
} // Dinic
};
class network_ableflow : protected network_maxflow
{
protected:
std::vector<ll> exf;
std::vector<ll> ret;
public:
network_ableflow()
{
exf.resize(maxn + 1);
ret.resize(maxm + 1);
}
void add_edge(pt_type u, pt_type v, ll b, ll c, unsigned id)
{
network_maxflow::add_edge(u, v, c - b, id);
exf[u] -= b;
exf[v] += b;
ret[id] += b;
}
std::vector<ll> solve(pt_type n)
{
pt_type s = n + 1, t = n + 2;
ll flow_s = 0, flow_t = 0;
for (pt_type i = 1; i <= n; i++)
{
if (exf[i] > 0)
{
network_maxflow::add_edge(s, i, exf[i], 0);
flow_s += exf[i];
}
else if (exf[i] < 0)
{
network_maxflow::add_edge(i, t, -exf[i], 0);
flow_t -= exf[i];
}
exf[i] = 0;
}
if (flow_s != flow_t)
throw std::runtime_error("Something wrong.");
auto mx_flow = network_maxflow::maxflow(s, t, n + 2);
// std::cerr << mx_flow << ' ' << flow_s << std::endl;
if (mx_flow != flow_s)
throw std::invalid_argument("N");
for (pt_type i = 1; i <= n; i++)
{
for (const auto &e : G[i])
{
const bian &p = G[e.v][e.fan_bian];
if (e.id == 0)
continue;
ret[e.id] += p.limit;
}
}
for (pt_type i = 1; i <= n; i++)
{
while (!G[i].empty() && (G[i].rbegin()->v == s || G[i].rbegin()->v == t))
G[i].pop_back();
}
G[s].clear();
G[t].clear();
return ret;
}
};
class network_biggestflow : public network_ableflow
{
public:
ll minflow(pt_type s_, pt_type t_, pt_type n_)
{
pt_type s = s_, t = t_, n = n_;
add_edge(t, s, 0, verybig_ll, 0);
solve(n);
// std::cerr << G[s].rbegin()->limit << std::endl;
G[t].rbegin()->limit = 0;
return G[s].rbegin()->limit - network_ableflow::maxflow(t, s, n);
}
} g;
int n, m, s, t;
int main()
{
#ifdef fre_io
freopen("in.in", "r", stdin);
freopen("in.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> n >> m >> s >> t;
for (int i = 1; i <= m; i++)
{
int u, v;
ll l, r;
std::cin >> u >> v >> l >> r;
g.add_edge(u, v, l, r, i);
}
try
{
auto zzz = g.minflow(s, t, n);
std::cout << zzz << std::endl;
}
catch (const std::invalid_argument &obj)
{
std::cout << obj.what() << std::endl;
}
return 0;
}
// Yeeart!!! 面向对象实际好用!4. 费用流
费用流一般指最小费用最大流(minimum cost maximum flow, MCMF)。
在原网络 的基础上,每条边有权值 。最小费用最大流要求保证最大流时 的最小值。 即每条边流单位流量的费用,我们需要在最大流的前提下最小化费用,因此该问题称为费用流。
4.1 无负圈的费用流
因为目前一般没人会卡,所以本文只讲述连续最短路(SSP, successive shortest path)算法。
想要使用 Primal-Dual 算法可以在学习 Johnson 算法后仿照其思路由连续最短路算法推出。
Push-Relabel 预流推进算法似乎没有费用流版本。
4.2 SSP 算法
将 Dinic 算法中的 BFS 换成 Bellman-ford,再把距离判定由边的数量改为边权和即可。
仿照一般的最大流,设 为整数,则其有时间复杂度上界 。
猜想
AC
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using ll = long long;
const ll maxn = 5e3;
const ll maxm = 5e4;
struct flow
{
template <class T>
T &qvmax(T &a, const T &b) { return ((a < b) ? (a = b) : a); }
template <class T>
T &qvmin(T &a, const T &b) { return ((b < a) ? (a = b) : a); }
static const int verybig_int = 0x3f3f3f3f, verybig_byte = 0x3f;
static const ll verybig_ll = 0x3f3f3f3f3f3f3f3f;
typedef unsigned pt_type;
struct bian
{
pt_type v;
ll limit, cost; // 残量网络 费用
std::size_t fan_bian;
};
std::vector<bian> G[maxn + 1];
void add_edge(pt_type u, pt_type v, ll limit_of_edge, ll cost_of_edge)
{
G[u].emplace_back(bian{v, limit_of_edge, cost_of_edge, G[v].size()});
G[v].emplace_back(bian{u, 0, -cost_of_edge, G[u].size() - 1});
}
pt_type s, t, n;
ll flow, cost;
bool vis[maxn + 1];
std::size_t cur[maxn + 1];
ll dis[maxn + 1];
bool bellman_ford()
{
std::queue<pt_type> q;
memset(vis, 0, sizeof vis);
memset(dis, verybig_byte, sizeof dis);
q.push(s);
dis[s] = 0;
vis[s] = 1;
while (!q.empty())
{
pt_type u = q.front();
q.pop();
vis[u] = 0;
for (const bian &b : G[u])
{
if (b.limit != 0 && dis[b.v] > dis[u] + b.cost)
{
dis[b.v] = dis[u] + b.cost;
if (!vis[b.v])
{
q.push(b.v);
vis[b.v] = 1;
}
}
}
}
memset(cur, 0, sizeof cur);
return (dis[t] != verybig_ll);
} // used SPFA
ll dfs(pt_type u, ll lim)
{
if (u == t)
return lim;
ll ret = 0;
vis[u] = 1;
for (std::size_t i = cur[u]; i < G[u].size(); i++)
{
cur[u] = i;
bian &b = G[u][i];
bian &p = G[b.v][b.fan_bian];
if (!vis[b.v] && b.limit != 0 && dis[b.v] == dis[u] + b.cost)
{
ll k = dfs(b.v, std::min(lim, b.limit));
if (k == 0)
dis[b.v] = verybig_int;
b.limit -= k;
p.limit += k;
ret += k;
lim -= k;
cost += k * b.cost;
if (lim == 0)
break;
}
}
vis[u] = 0;
return ret;
}
std::pair<ll, ll> maxflow(pt_type s_, pt_type t_, pt_type n_)
{
s = s_, t = t_, n = n_;
flow = 0;
cost = 0;
memset(vis, 0, sizeof vis);
while (bellman_ford())
{
flow += dfs(s, verybig_int);
// std::cout << flow << std::endl;
}
return std::make_pair(flow, cost);
} // Dinic
} g;
namespace fast_IO
{
constexpr uint32_t IOSIZE = 100000;
uint32_t precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; // ����ȡ����С������ precision λ
char obuf[IOSIZE], *p3 = obuf;
#if defined(ONLINE_JUDGE) or defined(FastIO)
char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;
#define getchar() ((p1 == p2) and (p2 = (p1 = ibuf) + fread(ibuf, 1, IOSIZE, stdin), p1 == p2) ? (EOF) : (*p1++))
#define putchar(x) ((p3 == obuf + IOSIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf), *p3++ = x)
#endif
template <typename T>
inline bool read(T &s)
{
s = 0;
int w = 1;
char ch;
while (ch = getchar(), !(ch > 47 and ch < 58) and (ch != EOF))
if (ch == '-')
w = -1;
if (ch == EOF)
return false;
while ((ch > 47 and ch < 58))
s = s * 10 + (ch - 48), ch = getchar();
return s *= w, true;
}
template <typename T>
inline void print(T x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + 48);
}
inline bool read(char &s)
{
while (s = getchar(), isspace(s) and s != EOF)
;
return s != EOF;
}
inline void print(char x) { putchar(x); }
inline bool read(char *s)
{
char ch;
while (ch = getchar(), (ch < 33) && ch != EOF)
;
if (ch == EOF)
return false;
while (!(ch < 33) and (ch != EOF))
*s++ = ch, ch = getchar();
*s = (char)0;
return true;
}
inline void print(char *x)
{
while (*x)
putchar(*x++);
}
inline void print(const char *x)
{
for (int i = 0; x[i]; i++)
putchar(x[i]);
}
inline bool read(std::string &s)
{
s = "";
char ch;
while (ch = getchar(), (ch < 33) and ch != EOF)
;
if (ch == EOF)
return false;
while (!(ch < 33))
s += ch, ch = getchar();
return true;
}
inline void print(std::string x)
{
for (int i = 0, n = x.size(); i < n; i++)
putchar(x[i]);
}
inline bool read(bool &b)
{
char ch;
while (ch = getchar(), (ch < 33) and ch != EOF)
;
return ch == EOF ? false : (b = ch ^ 48, true);
}
inline void print(bool b) { putchar(b + 48); }
inline bool read(double &x)
{
char ch = getchar();
int f = 1;
for (x = 0; (ch < 48 or 57 < ch) and (ch != EOF); ch = getchar())
if (ch == '-')
f = -1;
if (ch == EOF)
return false;
for (x = 0; 47 < ch and ch < 58; ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch != '.')
return x *= f, true;
double y = 0.1;
for (ch = getchar(); 47 < ch and ch < 58; ch = getchar())
x += y * (ch ^ 48), y /= 10;
return x *= f, true;
}
inline void print(double x)
{
if (x < 0)
putchar('-'), x = -x;
if (!precision)
print((unsigned long long)(x - (unsigned long long)(x) >= 0.5 ? x + 1 : x));
else
{
unsigned long long xx = x;
double y = ((x - xx) * POW[precision]);
unsigned long long yy = (y - (unsigned long long)(y) >= 0.5 ? y + 1 : y);
if (yy == POW[precision])
xx++, yy = 0;
print(xx), putchar('.');
for (int j = precision - 1; ~j; j--)
putchar(48 + yy / POW[j] % 10);
}
}
template <typename T, typename... T1>
inline int read(T &a, T1 &...other) { return read(a) + read(other...); }
template <typename T, typename... T1>
inline void print(T a, T1... other) { print(a), print(other...); }
template <typename T>
inline T read()
{
T a;
return read(a), a;
}
struct Fast_IO
{
bool flag = 1;
inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); }
inline void SetPrecision(int x)
{
if (x > 9)
throw std::runtime_error("Precision too high!");
else if (x < 0)
throw std::runtime_error("Precision too low!");
else
precision = x;
} // ������������Ϊ x������ȷ��С����� x λ��
inline operator bool() { return flag; }
} io;
Fast_IO &operator>>(Fast_IO &io, char *b) { return io.flag &= read(b), io; }
template <typename T>
Fast_IO &operator>>(Fast_IO &io, T &b) { return io.flag &= read(b), io; }
template <typename T>
Fast_IO &operator<<(Fast_IO &io, T b) { return print(b), io; }
constexpr char endl = '\n';
}
using fast_IO::io;
int n, m, s, t;
int main()
{
#ifdef fre_io
freopen("in.in", "r", stdin);
freopen("in.out", "w", stdout);
#endif
io >> n >> m >> s >> t;
for (int i = 1; i <= m; i++)
{
int u, v;
ll w, c;
io >> u >> v >> w >> c;
g.add_edge(u, v, w, c);
}
auto zzz = g.maxflow(s, t, n);
io << zzz.first << ' ' << zzz.second << fast_IO::endl;
return 0;
}消圈定理
残量网络 上无负圈当且仅当费用流 为当前流量下的最小费用流。
若存在负圈,则其总是可以在流量不变的情况下流满负圈;与其有最小费用相矛盾。同理,若其费用不为最小,则在流量恒定的情况下必然要求有负圈使得流量更小。
因此,初始网络 上无负圈时,算法过程中不会出现残量网络 上有负圈的情况。
4.3 有负圈的费用流
Wasshoi!
实际 Boss 战!
注意到负圈十分难以处理,因此考虑先使所有负边满流,图中就不会出现负圈了。
然而,使所有负边满流可能会破坏流的守恒性,因此需要仿照 3. 上下界网络流 引入 2.1 Push-Relabel 预流推进算法 中的 超额流概念建立虚拟源汇点进行预处理。此时需要将负边权费用纳入统计。
计算时,先设置超级源点 和超级汇点 ;再对于任意 连边 。
接着,求 的最小费用最大流,过程中只有费用需要纳入统计。这部分是为了满足流守恒性所做的最小费用的调整。
是否会出现无解的情况?
不可能,绝对不可能!
若 未满流,则总是可以将负边上的流退掉(经过其反边)以达成平衡,不会出现无解的情况。
最后,求 的最小费用最大流,其流量即为答案;费用即为之前所有累计费用的和。
AC
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using ll = long long;
const ll maxn = 1e3 + 3;
const ll maxm = 3e4;
class network_maxflow
{
protected:
template <class T>
T &qvmax(T &a, const T &b) { return ((a < b) ? (a = b) : a); }
template <class T>
T &qvmin(T &a, const T &b) { return ((b < a) ? (a = b) : a); }
static constexpr int verybig_int = 0x3f3f3f3f, verybig_byte = 0x3f;
static constexpr ll verybig_ll = 0x3f3f3f3f3f3f3f3f;
typedef unsigned pt_type;
struct bian
{
pt_type v;
ll limit; // 残量网络 费用
std::size_t fan_bian;
unsigned id;
ll cost;
};
std::vector<std::vector<bian>> G;
private:
pt_type s, t, n;
std::vector<bool> vis;
std::vector<std::size_t> cur;
std::vector<ll> dis;
public:
network_maxflow()
{
vis.resize(maxn + 1);
cur.resize(maxn + 1);
dis.resize(maxn + 1);
G.resize(maxn + 1);
}
void add_edge(pt_type u, pt_type v, ll limit_of_edge, unsigned id, ll cost)
{
G[u].emplace_back(bian{v, limit_of_edge, G[v].size(), id, cost});
G[v].emplace_back(bian{u, 0, G[u].size() - 1, 0, -cost});
}
bool bellman_ford()
{
std::queue<pt_type> q;
std::fill(vis.begin(), vis.end(), 0);
std::fill(dis.begin(), dis.end(), verybig_ll);
q.push(s);
dis[s] = 0;
vis[s] = 1;
while (!q.empty())
{
pt_type u = q.front();
q.pop();
vis[u] = 0;
for (const bian &b : G[u])
{
if (b.limit != 0 && dis[b.v] > dis[u] + b.cost)
{
dis[b.v] = dis[u] + b.cost;
if (!vis[b.v])
{
q.push(b.v);
vis[b.v] = 1;
}
}
}
}
std::fill(cur.begin(), cur.end(), 0);
return (dis[t] != verybig_ll);
} // used SPFA
ll dfs(pt_type u, ll lim, ll &cost)
{
if (u == t)
return lim;
ll ret = 0;
vis[u] = 1;
for (std::size_t i = cur[u]; i < G[u].size(); i++)
{
cur[u] = i;
bian &b = G[u][i];
bian &p = G[b.v][b.fan_bian];
if (!vis[b.v] && b.limit != 0 && dis[b.v] == dis[u] + b.cost)
{
ll k = dfs(b.v, std::min(lim, b.limit), cost);
if (k == 0)
dis[b.v] = verybig_int;
b.limit -= k;
p.limit += k;
ret += k;
lim -= k;
cost += k * b.cost;
if (lim == 0)
break;
}
}
vis[u] = 0;
return ret;
}
virtual std::pair<ll, ll> maxflow(pt_type s_, pt_type t_, pt_type n_)
{
s = s_, t = t_, n = n_;
ll flow = 0, cost = 0;
std::fill(vis.begin(), vis.end(), 0);
while (bellman_ford())
{
flow += dfs(s, verybig_int, cost);
// std::cout << flow << std::endl;
}
return std::make_pair(flow, cost);
} // Dinic
};
class network_negtive_circle_maxflow : protected network_maxflow
{
private:
std::vector<ll> exf;
ll pre_cost;
public:
network_negtive_circle_maxflow() : pre_cost(0)
{
exf.resize(maxn + 1);
}
void add_edge(pt_type u, pt_type v, ll limit_of_edge, ll c)
{
if (c >= 0)
network_maxflow::add_edge(u, v, limit_of_edge, 0, c);
else
{
network_maxflow::add_edge(v, u, limit_of_edge, 0, -c);
exf[u] -= limit_of_edge;
exf[v] += limit_of_edge;
pre_cost += limit_of_edge * c;
}
}
std::pair<ll, ll> maxflow(pt_type s, pt_type t, pt_type n) override
{
pt_type ss = n + 1, tt = n + 2;
ll flow = 0, cost = pre_cost;
for (pt_type i = 1; i <= n; i++)
{
if (exf[i] > 0)
network_maxflow::add_edge(ss, i, +exf[i], 0, 0);
if (exf[i] < 0)
network_maxflow::add_edge(i, tt, -exf[i], 0, 0);
exf[i] = 0;
}
std::pair<ll, ll> zzz = network_maxflow::maxflow(ss, tt, n + 2);
cost += zzz.second;
zzz = network_maxflow::maxflow(s, t, n);
flow += zzz.first;
cost += zzz.second;
zzz.first = flow;
zzz.second = cost;
return zzz;
}
} g;
int n, m, s, t;
int main()
{
#ifdef fre_io
freopen("in.in", "r", stdin);
freopen("in.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> n >> m >> s >> t;
for (int i = 1; i <= m; i++)
{
int x, y;
ll f, v;
std::cin >> x >> y >> f >> v;
g.add_edge(x, y, f, v);
}
auto zzz = g.maxflow(s, t, n);
std::cout << zzz.first << ' ' << zzz.second << std::endl;
return 0;
}
// 列位弟兄,随我接战,战至最后一刻,自刎归天!在多项式时间内将 Hamilton 回路问题归约为 Hamilton 回路问题
考虑枚举一条边 ,然后考虑判断从 到 的 Hamilton 路的存在性。也就是从 到 是否有长度为 的简单路径。 为了确保每个点都只用一次,考虑拆点。
连一条费用为 ,流量为 的边。对于原图的边 , 和 各连一条费用为 ,流量为 的边。
然后求从 到 的最小费用最大流,如果费用为 ,说明存在 Hamilton 路径。
因为所有边的流量都是 ,求出来的一定是单独的一条增广路不会有劈叉。而费用是 说明这条增广路一定经过了所有点,因此这条增广路就对应了原图中的 Hamilton 路。
注意到其中存在的问题是「经过 , 的环整体加上一个流量」。
没有负圈的费用流中,不经过 , 的流量必然是不优的。但存在负圈的费用流中可能会出现有流量不经过 ,,那么只要寻找到一条 到 的没有环的流即可。
找到一条没有环的流,即等价于其为一条简单路径并且经过所有 个点。又因为其为简单路径,所以其长度必然为 。
只需要将首尾两段连边,即可归约为经典的 Hamilton 回路问题!
因此,我们可以在多项式时间内将 Hamilton 回路问题归约为 Hamilton 回路问题!
然并卵
Hamilton 路和 Hamilton 回路的存在性问题属于 NP 完全问题,因此在目前的技术水平下都只存在指数级解法。
如果发现了其多项式级解法就意味着证明了 。(拿图灵奖的时候记得带我一个)
例题
To be continued.
关于此,我确信已发现了一套美妙的例题,可惜这里空白的地方太小,写不下。
可以在评论区推题,最好是同时带题目名称和链接,比如 P5192 Shoot the Bullet | 东方文花帖。
参考资料
- @starfallen, 题解:【模板】预流推进
- @bsdsdb, Push-Relabel 预流推进算法
- @Alex_Wei, 网络流
- @zhoumurui, Hamilton 回路的存在性!
- Tarjan, Analysis of the Highest-Label Push-Relabel Algorithm
- OI Wiki, 网络流
- Levent Tunqel, On the Complexity of Preflow-Push Algorithms for Maximum-Flow Problems
- @hrdgy, 较为实用的快速网络流 —— 倍增流量阈值优化 Dinic
- @LEWISAK, 题解:P3381 【模板】最小费用最大流
- @Hsl_Beat, 网络流基础(最大流,最小割,mcmf)|发现改标题不用重新审核
::anti-ai[本文作者是 clx201022,洛谷 UID 为 552688,原文:<https://www.luogu.com.cn/article/uzuz0xzo> 如果您能直接看到这段文字而在本页没有转载提示,或以“原创”名义发出,说明您访问的是侵权内容,请联系管理员删除文章并处罚侵权人,十分感谢您的见义勇为之举。]
创作声明
本文遵循 CC BY-SA 4.0 协议。
保证本文任何使用 AI 工具辅助创作之处都有明确标注。