最小费用最大流(MCMF) 模板
生活随笔
收集整理的這篇文章主要介紹了
最小费用最大流(MCMF) 模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
在圖問題中當最大流有多組解時,給每條邊在附上一個單位費用的量,問在滿足最大流時的最小費用是多少?
思路
蒟蒻在這里套用一下博主STILLxjy的說法:
給出一個容量網絡,那他的最大流一定是一個定值(即使是有多個一樣的最大值)。所以我們從開始的可行流開始增廣時,最終的增廣量是一定的。所以為了滿足最小費用我們只需要每次找最小費用的增廣路即可,直到流量為最大值。這個問題僅僅是在求增廣路時先考慮費用最小的增廣路,其他思想和EK思想一樣。
我們學過SPFA求最短路算法(bellman-ford的隊列優化),所以我們將弧的費用看做是路徑長度,即可轉化為求最短路的問題了。只需要所走的最短路滿足兩個條件即可:1可增廣cap> flow,2路徑變短d[v]>d[u]+cost< u,v> 。
模板如下:
劉汝佳的紫書模板,容易TLE,不適用于點多邊少的情況:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 210;
const int maxm = 220;
struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w):from(u),to(v),cap(c),flow(f),cost(w)
{}
};
struct MCMF {
int n, m;
vector<Edge> edges; //保存表
vector<int> G[maxn]; ////保存鄰接關系
int inq[maxn]; //是否在隊列中,用于SPFA算法
int d[maxn]; //源點到i的最短路徑估計值
int p[maxn]; //記錄路徑,記錄上一條弧
int a[maxn]; //找到增廣路徑后的改進量
MCMF() {}
void init(int n) {
this->n = n;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
void addEdge(int from, int to, int cap, int cost) {
edges.push_back(Edge(from, to, cap, 0, cost)); //正向
edges.push_back(Edge(to, from, 0, 0, -cost)); //反向
m = edges.size();
G[from].push_back(m-2); //按照邊的編號保存鄰接關系
G[to].push_back(m-1);
}
//隊列優化的SPFA算法
//尋找最小費用的增廣路,使用引用同時修改原flow,cost
bool bellmanFord(int s, int t, int& flow, long long& cost) {
for(int i = 0; i < n; i++) d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
queue<int> Q;
Q.push(s);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
//尋找滿足容量大于流量的可松弛邊
if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
//是否在隊列中
if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
}
}
}
if(d[t] == INF) return false; //如果d[t]沒有被更新,相當于沒找到增廣路徑,則沒有最大流也沒有最小費用
flow += a[t]; //更新最大流
cost += (long long)d[t] * (long long)a[t]; ;//單位流量乘以單位路徑長度用來計算消耗
//通過使用p[]保存的上一個邊的值來對剛剛找到的增廣路徑上面的流量進行更新
for(int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t]; //正向邊更新
edges[p[u]^1].flow -= a[t];} //反向邊更新(用位運算實現的)
return true;
}
//需要保證初始網絡中沒有負權圈
//計算從s到t的最小消耗cost,返回最大流
int mincostMaxflow(int s, int t, long long& cost) {
int flow = 0; cost = 0;
while(bellmanFord(s, t, flow, cost));//不斷尋找最短增廣路徑,直到找不到為止
return flow;
}
}g;
int main(void) {
int nn, mm, s, t, u, v, cap, cost, totFlow = 0;
long long totCost = 0;
while(~scanf("%d%d", &nn, &mm)) {
s = 1;
t= nn;
g.init(t+1);
for (int i = 0; i < mm; i++) {
scanf("%d%d%d%d", &u, &v, &cap, &cost);
g.addEdge(u, v, cap, cost);
}
totFlow = g.mincostMaxflow(1, t, totCost);
printf("最小花費:%lld 最大流:%d
", totCost, totFlow);
}
return 0;
}
View Code
劉汝佳的白書模板,不用STL,很穩:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 210;
const int maxm = 220;
struct Edge {
int from, to, cap, flow, cost;
Edge () {} //作為Edge類數組的默認構造函數
Edge (int u, int v, int ca, int fl, int co):from(u), to(v), cap(ca), flow(fl), cost(co)
{}
};
struct MCFC {
int n, m, s, t;
Edge edges[maxm];
int first[maxn];
int next[maxm];
int inq[maxn];
int d[maxm];
int p[maxn];
int a[maxn];
int Q[maxn];
MCFC() {}
void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
}
void addEdge(int u, int v, int cap, int cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
}
bool bellmanFord(int s, int t, int &flow, long long &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;
int front, rear;
Q[rear = front = 0] = s;
while (front <= rear) {
int u = Q[front++];
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
Edge &e = edges[i];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = i;
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) {Q[++rear] = e.to; inq[e.to] = true;}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += (long long)d[t] * (long long)a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int mincostMaxflow(int s, int t, long long& cost) {
int flow = 0; cost = 0;
while (bellmanFord(s, t, flow, cost));
return flow;
}
}g;
int main(void) {
int nn, mm, s, t, u, v, cap, cost, totFlow = 0;
long long totCost = 0;
while(~scanf("%d%d", &nn, &mm)) {
s = 1;
t= nn;
g.init(t+1);
for (int i = 0; i < mm; i++) {
scanf("%d%d%d%d", &u, &v, &cap, &cost);
g.addEdge(u, v, cap, cost);
}
totFlow = g.mincostMaxflow(1, t, totCost);
printf("最小花費:%lld 最大流:%d
", totCost, totFlow);
}
return 0;
}
View Code
————全心全意投入,拒絕畫地為牢
總結
以上是生活随笔為你收集整理的最小费用最大流(MCMF) 模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人民币小写转大写
- 下一篇: 雉子筵片_功效作用注意事项用药禁忌用法用