Minimum cost flow of network flow

Source: Internet
Author: User

In the network of the maximal flow problem, the new cost is added to the edge, and the minimum value of the cost is calculated for the flow of F. This kind of problem is the minimum flow cost problem.

Algorithm analysis: In solving the problem of minimum cost flow, we will follow the shortest path augmentation and take the cost as the length of the measurement, the residual network in the augmentation of the reverse side of the cost should be the inverse of the original side costs, the purpose is to ensure that the process is reversible and correct. Therefore, in the implementation of this algorithm, we need to use the Bellman_ford or Dijkstra algorithm to obtain the shortest path and save it, the second is to solve the actual traffic through the shortest path and modify the relevant data, so that the next time on the residual network to continue to augment. If Bellman_ford is used to find the shortest path, the time complexity is O (FVE), and if Dijkstra is used, the time complexity is O (FELOGV).

Proof of the correctness of the algorithm:

(1) Set the cost flow obtained by the above algorithm to F and assume that there is a flow of the same but less expensive stream f '. In known streams F and f ', the inflow of other vertices is equal to the outflow, except S and T. In the flow F '-F, the flow F '-F is composed of several loops and at least one negative circle, since F is the same as F ' and the cost of F ' is less than F. Then we can get a conclusion: if f is the minimum cost flow, then there is no negative circle in the network, and vice versa.

(2) using the conclusions in (1), we will prove by inductive method. Set F (i) is the flow of all flows to the lowest cost of the stream, then when the i=0, f (i) = 0, the corresponding residual network is the original, if the original image does not contain a negative circle, f (0) is the flow of 0 of the minimum cost flow. Assuming that the flow is I, f (i) is the minimum cost flow, and the cost of the flow to i+1 has been calculated by the algorithm is F (i+1), then F (i+1)-F (i) is f (i) corresponding to the residual network s-t the shortest.

(3) Suppose that F (i+1) is not a minimum cost flow, i.e. there is a less expensive stream F ' (i+1). In Flow F ' (i+1)-F (i), the inflow of other vertices, except s and T, is equal to the outflow, and thus is a path of S to T and several loops. Known

F (i+1)-F (i) is the s-t of the residual network of f (i), and F ' (i+1) is less than F (i+1), so f ' (i+1)-F (i) contains at least one negative ring, f (i) corresponding to the residual network contains a negative ring. This result and f (i) is the least cost flow contradiction, it is assumed that not immediately F (i+1) is the least cost flow.

Minimum cost flow Algorithm implementation:

Bellman-ford algorithm

Bellman algorithm for shortest augmented path & minimum cost flow O (FEV)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace Std;

#define MV 100
#define INF (1 << 20)
struct Edge
{
int T, CAP, cost, rev;
Edge (int to = 0, int c = 0, int ct = 0, int r = 0): t (to), Cap (c), Cost (CT), rev (R) {};
};
adjacency table representation of vector <edge> g[mv];//graphs
int dis[mv];//the shortest distance from a single source point s to other vertices (minimum cost for this search)
int PREVV[MV], preve[mv];//the corresponding edge of the precursor node in the shortest circuit

int Min_cost_flow (int n, int v, int s, int t, int f)
{
int ans = 0, I, J; Minimum cost
while (F > 0)
{
Fill (dis, dis + V, INF);
Dis[s] = 0

BOOL update = TRUE;
while (update)
{The//bellman algorithm solves the shortest possible path in the S-T (lowest cost) and whether it is up to the e.c whether it is greater than 0
Update = FALSE;
for (i = 0; i < V; ++i)
{
int size = G[i].size ();
if (dis[i] = = INF)
Continue

for (j = 0; j < size; ++j)
{
Edge &es = G[i][j];
if (Es.cap > 0 && dis[es.t] > Dis[i] + es.cost)
{
DIS[ES.T] = dis[i] + es.cost;
PREVV[ES.T] = i;//path restore
PREVE[ES.T] = j;

Update = TRUE;
}
}
}
}

if (dis[t] = = INF)
return-1;
int d = f; D: The minimum cost flow for this time
for (i = t; I! = s; i = Prevv[i])
d = min (d, g[prevv[i]][preve[i]].cap);

Ans + = d * Dis[t];
F-= D;
for (i = t; I! = s; i = Prevv[i])
{
Edge &es = G[prevv[i]][preve[i]];
Es.cap-= D;
G[es.t][es.rev].cap + = D;

}
}
return ans;
}

void Solve (int n, int v, int s, int t, int f)
{
int I, J;
for (i = 0; i < n; ++i)
{//Build diagram
int s1, T1, cap, cost;
CIN >> S1 >> T1 >> cap >> cost;
G[s1].push_back (Edge (T1, cap, cost, g[t1].size ()));
G[t1].push_back (Edge (S1, 0,-cost, G[s1].size ()-1));

}

cout << min_cost_flow (n, V, S, T, f) << Endl;
}

int main ()
{
int N, V, S, T, f;//n Edge v Vertex Source point S meeting point T Transport traffic F
CIN >> N >> v >> s >> T >> F;
Solve (n, V, S, T, F);
return 0;
}

Dijkstra algorithm for minimum cost flow

Dijkstra algorithm to find the minimum cost stream core code:

H[MAX_V]: Import potential ensures all edges are non-negative edge O (FELOGV)
int Min_cost_flow (int n, int v, int s, int t, int f)
{

int i, ans = 0;

Fill (H, H + V, 0);

while (F > 0)
{
Dijkstra algorithm: Finding the shortest circuit O (ELOGV)
Priority_queue<p, Vector<p>, greater<p> > que;
Fill (dis, dis + V, INF);
Dis[0] = 0;
Que.push (P (0, s));
while (!que.empty ())
{
P p = que.top ();
Que.pop ();

int v = p.second;
if (Dis[v] < P.first)
Continue
int size = G[v].size ();

for (i = 0; i < size; ++i)
{
Edge es = g[v][i];//****
if (Es.cap > 0 && dis[es.to] > Dis[v] + es.cost + h[v]-h[es.to])

{

Dis[es.to] = Dis[v] + es.cost + h[v]-h[es.to];

Prevv[es.to] = v;
Preve[es.to] = i;
Que.push (P (dis[es.to], es.to));
}
}
}

if (dis[t] = = INF)
return-1;
Update potential
for (i = 0; i < V; ++i)
H[i] + = dis[i];
int d = f;
for (i = t; I! = s; i = Prevv[i])
d = min (d, g[prevv[i]][preve[i]].cap);
Ans + = d * H[t];

F-= D;

for (i = t; I! = s; i = Prevv[i])
{
Edge &es = G[prevv[i]][preve[i]];
Es.cap-= D;
G[i][es.rev].cap + = D;
}
}

return ans;
}

Minimum cost flow of network flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.