有向圖的最小費用最大流問題
- 預備知識:最大流問題,增廣路算法。
- 問題說明:把物品從結點s(稱爲源點)運送至結點t(稱爲匯點),每條邊上有一個二元組(x,y),x表示邊的最大運送能力,y表示運送單位物品的花費。尋找總流量最大的前提下(或者把結點s上的物品全部運送至結點t),總費用最小的流。
- 算法思想:
1、在源點s處增加一個”超級源點S”,在匯點t處增加一個”超級匯點T”,其中S->s,T->t,且這兩條邊運送單位物品的花費爲0。
2、最大流:殘量網絡中一條從s到t的有向道路對應原圖中的一條增廣路,求出該道路中所有殘量的最小值chang[t],把該道路中所有邊的當前流量(若從零流開始,則所有邊的當前流量爲0)增加chang[t],即求出一條從s到t的有向道路的最大流,依次將從s到t的所有有向道路的最大流求出。
3、最小費用最大流:只要初始流是該流量下的最小費用可行流,且每次增廣後的新流都是新流量下的最小費用流即可。
附錄代碼,可以直接運行得到結果
#define _CRT_SECURE_NO_WARNINGS
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef struct
{
int from, to, cap, flow, cost;
void getinf(int u, int v, int c, int f, int w)
{
from = u; to = v; cap = c; flow = f; cost = w;
}
}Edge; //邊信息
typedef struct{
int Path_flow; //路徑的最大流量
vector<int> Path; //路徑所經過的結點ID
}OnePath; //源點到匯點的一條路徑的信息
class MCMF
{
public:
int node_num; //結點個數
int edge_num; //邊的個數
vector<Edge> Edges; //存儲邊信息,邊數的兩倍,一條有向邊由正向弧(正向弧爲這條邊本身)和反向弧組成
vector<int> *G; //鄰接表,G[i][j]表示結點i的第j條邊在edges中的序號
int *vis; //標記矩陣,是否在隊列中
int *dis; //Bellman-Ford
int *father; //上一條弧
int *change; //可改進量
vector<OnePath> res_path; //保存路徑結果,以及對應路徑上的總流量
public:
//初始化函數
void init(int node_num, int edge_num)
{
this->node_num = node_num;
this->edge_num = edge_num;
this->G = new vector<int> [node_num];
this->vis = new int [node_num];
this->dis = new int [node_num];
this->father = new int [node_num];
this->change = new int [node_num];
}
//存儲邊信息,一條邊的正向弧和反向弧保存在一起,即ID爲0和1的弧互爲反向弧,ID爲2和3的弧互爲反向弧...弧i的反向弧爲i^1,其中^爲二進制異或運算符
void Add_Edge(int from,int to,int cap,int cost)
{
Edge edge;
edge.getinf(from, to, cap, 0, cost);
this->Edges.push_back(edge); //正向弧
edge.getinf(to, from, 0, 0, -cost);
this->Edges.push_back(edge); //反向弧
int len = Edges.size();
this->G[to].push_back(len-1);
this->G[from].push_back(len-2);
}
//BellmanFord算法,最小費用最大流
bool BellmanFord(int s, int t, int& flow, int& cost)
{
for(int i=0; i<node_num; i++)
this->vis[i] = 0;
for(int i=0; i<node_num; i++)
this->dis[i] = INF;
dis[s] = 0;
vis[s] = 1;
father[s] = -1;
change[s] = INF;
queue<int> q;
q.push(s);
while( !q.empty() )
{
int u = q.front();
q.pop();
vis[u] = 0;
for(int i=0; i<G[u].size(); i++)
{
Edge& e = Edges[G[u][i]];
if( e.cap>e.flow && dis[e.to]>dis[u]+e.cost )
{
dis[e.to] = dis[u] + e.cost;
father[e.to] = G[u][i];
change[e.to] = min(change[u], e.cap-e.flow);
if(vis[e.to] == 0)
{
vis[e.to] = 1;
q.push(e.to);
}
}
}
}
if(dis[t] == INF)
return false;
flow += change[t];
cost += dis[t]*change[t];
//change[t]爲當前路徑的總流量
OnePath temp_one_path;
temp_one_path.Path_flow = change[t];
for(int u = t; u!=s ; u = Edges[father[u]].from)
{
Edges[father[u]].flow += change[t];
Edges[father[u]^1].flow -= change[t];
//u即爲路徑上的點,此處是反向的
if(u==t)
continue;
else
temp_one_path.Path.push_back(u);
}
res_path.push_back(temp_one_path);
return true;
}
//最小費用最大流
//maxflow:需要從源點s運送至匯點t的全部物品。當問題爲計算從s到t的最大流時,不需要maxflow;當問題爲將全部物品maxflow從s運送至t時,需要maxflow。
int MinCostMaxFlow(int s, int t, int maxflow)
{
int flow = 0, cost = 0;
while(BellmanFord(s, t, flow, cost));
if(flow < maxflow)
return -1;
else
return cost;
}
//析構函數釋放內存
~MCMF()
{
Edges.clear();
for(int i=0; i<node_num; i++)
G[i].clear();
delete [] G;
delete [] vis;
delete [] dis;
delete [] father;
delete [] change;
}
};
void main()
{
MCMF mcmf_sample;
int node_num, edge_num; //輸入結點個數(包含源點和匯點),邊的個數
cout << "input node_num and edge_num: ";
cin >> node_num >> edge_num;
mcmf_sample.init(node_num+2, edge_num);
int from, to, cap, cost; //輸入每條邊的信息:起點,終點,邊的最大運送能力,單位花費
cout << "input from, to, cap, cost:" << endl;
for(int i=0; i<edge_num; i++)
{
cin >> from >> to >> cap >> cost;
mcmf_sample.Add_Edge(from, to, cap, cost);
}
int s, t; //源點,匯點
int maxflow; //maxflow:需要從源點s運送至匯點t的全部物品。當問題爲計算從s到t的最大流時,不需要maxflow;當問題爲將全部物品maxflow從s運送至t時,需要maxflow。
cout << "input s, t, maxflow: ";
cin >> s >> t >> maxflow;
int S = node_num; //超級源點
int T = node_num+1; //超級匯點
mcmf_sample.Add_Edge(S, s, maxflow, 0);
mcmf_sample.Add_Edge(t, T, maxflow, 0);
cout << "totle cost: " << mcmf_sample.MinCostMaxFlow(S, T, maxflow) << endl; //輸出最小費用
for(vector<OnePath>::iterator it=mcmf_sample.res_path.begin(); it!=mcmf_sample.res_path.end(); it++)
{
cout << "current path flow: " << it->Path_flow << endl; //輸出當前路徑的總流量
cout << "current path flow: "; //輸出當前路徑
for(int i=it->Path.size()-1; i>=0; i--)
cout << it->Path[i] << ' '; //輸出s到t的路徑
cout << endl;
}
}