P7173 【模板】有负圈的费用流 题解
Description
给出一张有向图,求最小费用最大流。费用可能为负。
Solution
如果费用为负,SSP 类算法在求最短路的时候就会因为负环卡死。因此可以这么想。
对于一条边
具体的,可以使用上下界网络流。对于新添的的一条边
上下界网络流
先让边的下界满流,虚拟超级源汇
Code
#include <bits/stdc++.h>
using namespace std;
class Dinic {
public:
typedef int TYPE;
static const int maxn = 1e4, inf = 0x7f7f7f7f, maxm = inf;
class edge {
public:
int to, rev;
TYPE flow, cost;
edge(int to, TYPE flow, TYPE cost, int rev): to(to), flow(flow), cost(cost), rev(rev) {}
edge() {}
};
std::vector<edge>vec[maxn];
TYPE MinCost, MaxFlow, dist[maxn], W[maxn];
int cur[maxn], n, m, s, t;
bool vis[maxn];
Dinic(int n, int m, int s, int t): n(n), m(m), s(s), t(t),
MinCost(0), MaxFlow(0), cur({0}), dist({0}), vis({false}) {}
Dinic() {}
inline void _Add_Edge_(int from, int to, TYPE flow, TYPE cost) {
vec[from].push_back(edge(to, flow, cost, vec[to].size()));
vec[to].push_back(edge(from, 0, -cost, vec[from].size() - 1));
}
inline void Add_Edge(int from, int to, TYPE flowL, TYPE flowR, TYPE cost) {
W[from] -= flowL, W[to] += flowL;
MinCost += flowL * cost;
_Add_Edge_(from, to, flowR - flowL, cost);
}
inline bool SPFA() {
std::memset(dist, inf, sizeof(dist));
dist[t] = 0;
std::memset(cur, 0, sizeof(cur));
std::queue<int>que;
for (que.push(t), vis[t] = true; !((bool) que.empty());) {
int u = que.front();
vis[u] = false;
que.pop();
for (edge &e : vec[u]) {
int v = e.to;
if (vec[v][e.rev].flow && dist[u] + vec[v][e.rev].cost < dist[v]) {
dist[v] = dist[u] + vec[v][e.rev].cost;
if (!vis[v]) {
que.push(v);
vis[v] = true;
}
}
}
}
return dist[s] != inf;
}
inline TYPE dfs(int u, TYPE flow) {
if (u == t || !flow) {
return flow;
}
vis[u] = true;
TYPE res = 0;
int sz = vec[u].size();
for (int &i = cur[u]; i < sz && flow; ++i) {
edge &e = vec[u][i];
int v = e.to;
if (e.flow && dist[u] == dist[v] + e.cost && !vis[v]) {
TYPE Preflow = dfs(v, std::min(flow, e.flow));
res += Preflow, flow -= Preflow, MinCost += Preflow * (e.cost);
e.flow -= Preflow, vec[v][e.rev].flow += Preflow;
}
}
vis[u] = false;
return res;
}
inline TYPE Get_MaxFlow_MinCost() {
MaxFlow = 0;
while (SPFA()) {
std::memset(vis, false, sizeof(vis));
TYPE Preflow = dfs(s, inf);
MaxFlow += Preflow;
}
return MaxFlow;
}
inline TYPE clac(int S, int T) {
Add_Edge(T, S, 0, inf, 0);
int PreS = n + 1, PreT = n + 2;
this->s = PreS, this->t = PreT;
TYPE sum = 0;
for (int i = 0; i <= n; ++i) {
if (W[i] > 0) {
Add_Edge(PreS, i, 0, W[i], 0);
sum += W[i];
} else if (W[i] < 0) {
Add_Edge(i, PreT, 0, -W[i], 0);
}
}
if (Get_MaxFlow_MinCost() < sum) {
return -1;
} else {
TYPE res = vec[S].back().flow;
vec[S].pop_back();
vec[T].pop_back();
this->s = S, this->t = T;
res = Get_MaxFlow_MinCost();
return (MaxFlow = res);
}
}
inline void Solve() {
int N, M, S, T;
scanf("%d%d%d%d", &N, &M, &S, &T);
this[0] = Dinic(N, M, S, T);
for (int i = 1; i <= M; ++i) {
int u, v;
TYPE flow, cost;
scanf("%d%d%d%d", &u, &v, &flow, &cost);
if (cost < 0) {
W[u] -= flow, W[v] += flow;
MinCost += cost * flow;
Add_Edge(v, u, 0, flow, -cost);
} else {
Add_Edge(u, v, 0, flow, cost);
}
}
clac(S, T);
printf("%d %d\n", MaxFlow, MinCost);
}
};
Dinic Main;
int main() {
Main.Solve();
return 0;
}