一、问题介绍
最小费用路算法是寻找源点到汇点的最小费用路,也就是以费用为边权,找最短路,然后对找到的最短路进行增流,以此反复,需要注意的是,在求解最大流时,最短增广路算法中的最短增广路是去权值的最短路,而最小费用路是以单位费用为权值的最短路,一般分成求出最小费用路和沿最小费用路增流,可以选用SPFA+EK=MCMF的方式,这种方式便于处理负权边,也可以采用Dijkstra+Dinic/ISAP,但一般由于负权边的关系,最小费用最大流(Minimum Cost Maximum Flow, MCMF)是一种在网络流问题中找到最小费用最大流量的算法。它结合了最大流和最短路径算法,通常使用贝尔曼-福特算法或迪杰斯特拉算法来实现。
二、算法实现思路
在最小费用最大流中,网络中需要考虑的不仅是流量了,还涉及到单位流量流过的费用,网络流的费用成为了每条边的流量×单位流量的费用,即C o s t ( f l o w ) = ∑ < x , y > ∈ E c o s t ( x , y ) × f l o w ( x , y ) Cost(flow)=\sum_{<x,y>\in E}cost(x,y)×flow(x,y)Cost(flow)=∑
<x,y>∈E cost(x,y)×flow(x,y),求解最小费用最大流有两个思路:
- 先找最小单位费用路,之后在该路径上增流并增加到最大流,最小费用路算法。
- 先找最大流,然后找负费用圈,消减费用,减少到最小费用,消圈算法
三、代码实现
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005; // 最大顶点数
const int MAXM = 10005; // 最大边数
const int INF = 0x3f3f3f3f; // 无穷大
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]; // 是否在队列中
int d[MAXN]; // Bellman-Ford算法中的距离数组
int p[MAXN]; // 上一条弧
int a[MAXN]; // 可改进量
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);
}
bool BellmanFord(int s, int t, int &flow, int &cost) {
for (int i = 0; i < n; i++) {
d[i] = INF;
}
memset(inq, 0, sizeof(inq));
d[s] = 0;
inq[s
1573

被折叠的 条评论
为什么被折叠?



