保证最大流的前提下最小费用
原始对偶算法实现
O(nm+mlogmf)
constexpr int Max=1e18;
struct MCFGraph {
struct Edge {
int v, c, f;
Edge(int v, int c, int f) : v(v), c(c), f(f) {}
};
const int n;
vector<Edge> e;
vector<vector<int>> g;
vector<int> h, dis;
vector<int> pre;
bool dijkstra(int s, int t) {
dis.assign(n, Max);
pre.assign(n, -1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
dis[s] = 0;
que.emplace(0, s);
while (!que.empty()) {
pair<int, int>now = que.top();
int d = now.first, u = now.second;
que.pop();
if (dis[u] < d) continue;
for (int i : g[u]) {
Edge NOW = e[i];
int v = NOW.v, c = NOW.c, f = NOW.f;
if (c > 0 && dis[v] > d + h[u] - h[v] + f) {
dis[v] = d + h[u] - h[v] + f;
pre[v] = i;
que.emplace(dis[v], v);
}
}
}
return dis[t] != Max;
}
MCFGraph(int n) : n(n), g(n) {}
void addEdge(int u, int v, int c, int f) {
g[u].push_back(e.size());
e.emplace_back(v, c, f);
g[v].push_back(e.size());
e.emplace_back(u, 0, -f);
}
pair<int, int> flow(int s, int t) {
int flow = 0;
int cost = 0;
h.assign(n, 0);
while (dijkstra(s, t)) {
for (int i = 0; i < n; ++i) h[i] += dis[i];
int aug = Max;
for (int i = t; i != s; i = e[pre[i] ^ 1].v) aug = min(aug, e[pre[i]].c);
for (int i = t; i != s; i = e[pre[i] ^ 1].v) {
e[pre[i]].c -= aug;
e[pre[i] ^ 1].c += aug;
}
flow += aug;
cost += (int)aug * h[t];
}
return {flow, cost};
}
};O(nmf)
基于dinic实现:
struct MCFGraph{
//#define int long long
static constexpr int inf = 1e18;
struct Edge {
int v, c, f;
Edge(int v, int c, int f) : v(v), c(c), f(f) {}
};
int n;
vector<Edge> e;
vector<vector<int>> g;
vector<int> dis, cur, vis;
MCFGraph(int n){
this->n = n;
e.clear();
g.assign(n, {});
cur.assign(n, 0);
vis.assign(n, 0);
}
void add(int u, int v, int c, int f){
g[u].push_back(e.size());
e.emplace_back(v, c, f);
g[v].push_back(e.size());
e.emplace_back(u, 0, -f);
}
bool bfs(int s, int t){
dis.assign(n, inf);
queue<int> que;
dis[s] = 0;
vis[s] = 1;
que.push(s);
while(!que.empty()){
int u = que.front();
que.pop();
vis[u] = 0;
for(auto i : g[u]){
auto [v, c, f] = e[i];
if(c > 0 && dis[v] > dis[u] + f){
dis[v] = dis[u] + f;
if(!vis[v]){
vis[v] = 1;
que.push(v);
}
}
}
}
return dis[t] != inf;
}
int dfs(int u, int t, int flow){
if (u == t) return flow;
vis[u] = 1;
int used = 0;
for(int &i = cur[u]; i < (int)g[u].size(); i++){
int j = g[u][i];
auto [v, c, f] = e[j];
if(!vis[v] && c > 0 && dis[v] == dis[u] + f){
int k = dfs(v, t, min(flow - used, c));
used += k;
e[j].c -= k;
e[j ^ 1].c += k;
if(used == flow) break;
}
}
vis[u] = 0;
return used;
}
pair<int, int> flow(int s, int t){
int flow = 0;
int cost = 0;
while(bfs(s, t)){
cur.assign(n, 0);
int aug = dfs(s, t, inf);
flow += aug;
cost += dis[t] * aug;
}
return {flow, cost};
}
};保证费用最小 流不保证最大
template <class T = int>
class MCMF{
private:
struct Edge{
int to;
T cap, cost;
Edge(int a, T b, T c) : to(a), cap(b), cost(c) {}
};
int n;
vector<vector<int>> edges;
vector<Edge> list;
vector<int> from;
vector<T> dist, pot;
vector<bool> visit;
pair<T, T> augment(int src, int sink){
pair<T, T> flow = {list[from[sink]].cap, 0};
for (int v = sink; v != src; v = list[from[v] ^ 1].to){
flow.first = std::min(flow.first, list[from[v]].cap);
flow.second += list[from[v]].cost;
}
for (int v = sink; v != src; v = list[from[v] ^ 1].to){
list[from[v]].cap -= flow.first;
list[from[v] ^ 1].cap += flow.first;
}
return flow;
}
queue<int> q;
bool SPFA(int src, int sink){
T INF = numeric_limits<T>::max();
dist.assign(n, INF);
from.assign(n, -1);
q.push(src);
dist[src] = 0;
while (!q.empty()){
int on = q.front();
q.pop();
visit[on] = false;
for (auto e : edges[on]){
auto ed = list[e];
if (ed.cap == 0)
continue;
T toDist = dist[on] + ed.cost + pot[on] - pot[ed.to];
if (toDist < dist[ed.to]){
dist[ed.to] = toDist;
from[ed.to] = e;
if (!visit[ed.to]){
visit[ed.to] = true;
q.push(ed.to);
}
}
}
}
return dist[sink] < INF;
}
bool dij(int src, int sink) {
T INF = std::numeric_limits<T>::max();
dist.assign(n, INF);
from.assign(n, -1);
visit.assign(n, false);
dist[src] = 0;
for(int i = 0; i < n; i++) {
int best = -1;
for(int j = 0; j < n; j++) {
if(visit[j]) continue;
if(best == -1 || dist[best] > dist[j]) best = j;
}
if(dist[best] >= INF) break;
visit[best] = true;
for(auto e : edges[best]) {
auto ed = list[e];
if(ed.cap == 0) continue;
T toDist = dist[best] + ed.cost + pot[best] - pot[ed.to];
assert(toDist >= dist[best]);
if(toDist < dist[ed.to]) {
dist[ed.to] = toDist;
from[ed.to] = e;
}
}
}
return dist[sink] < INF;
}
void fixPot(){
T INF = numeric_limits<T>::max();
for (int i = 0; i < n; i++){
if (dist[i] < INF)
pot[i] += dist[i];
}
}
public:
MCMF(int size){
n = size;
edges.resize(n);
pot.assign(n, 0);
dist.resize(n);
visit.assign(n, false);
}
pair<T, T> solve(int src, int sink){
pair<T, T> ans(0, 0);
// Remove negative edges: Johnson's Algorithm
if (!SPFA(src, sink))
return ans;
fixPot();
// Can use dijkstra to speed up depending on the graph
while (SPFA(src, sink)){
auto flow = augment(src, sink);
// When the priority is the minimum cost and not the flow
if(flow.second >= 0)
break;
ans.first += flow.first;
ans.second += flow.first * flow.second;
fixPot();
}
return ans;
}
void addEdge(int u, int to, T cap, T cost){
edges[u].push_back(list.size());
list.push_back(Edge(to, cap, cost));
edges[to].push_back(list.size());
list.push_back(Edge(u, 0, -cost));
}
};