Given a source node **S, ** a sink node **T**, two matrices** Cap[ ][ ]** and **Cost[ ][ ] **representing a graph, where **Cap[i][j] **is the capacity of a directed edge from node **i** to node **j** and **cost[i][j]** is the cost of sending one unit of flow along a directed edge from node **i** to node **j**, the task is to find a flow with the minimum-cost maximum-flow possible from the given graph.

Minimum Cost Maximum Flow:Minimum cost(per unit of flow) required to deliver maximum amount of flow possible from the given graph.

**Example:**

Input:S = 0, T = 4, cap[ ][ ] = {{0, 3, 4, 5, 0}, {0, 0, 2, 0, 0}, {0, 0, 0, 4, 1}, {0, 0, 0, 0, 10}, {0, 0, 0, 0, 0}},

cost[ ][ ] = {{0, 1, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}

Output:10 1

Explanation:

For given graph, Max flow = 10 and Min cost = 1.

Input:S = 0, T = 4, cost[][] = { { 0, 1, 0, 0, 2 }, { 0, 0, 0, 3, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0 } }

cap[][] = { { 0, 3, 1, 0, 3 }, { 0, 0, 2, 0, 0 }, { 0, 0, 0, 1, 6 }, { 0, 0, 0, 0, 2 }, { 0, 0, 0, 0, 0 } }

Output:6 8

**Approach:**

Negative cycle in the cost network is cycled with the sum of costs of all the edges in the cycle is negative. They can be detected using Bellman Ford algorithm. They should be eliminated because, practically, flow through such cycles cannot be allowed. Consider a **negative cost cycle**, if all flow has to pass through this cycle, the total cost is always reducing for every cycle completed. This would result in an infinite loop in the desire of **minimizing the total cost**. So, whenever a cost network includes a negative cycle, it implies, the cost can further be minimized (by flowing through the other side of the cycle instead of the side currently considered). A negative cycle once detected are removed by flowing a Bottleneck Capacity through all the edges in the cycle.

Now, look at what supply and demand nodes are:

Supply nodes:These are positive Nodes that are added to the flow and which produces the flow.

Demand nodes:These are negative nodes which are subtracted from the flow.

Supply (or demand) at each node= Total flow leading out of the Node – The total flow leading into the Node

The given problem is approached by sending a bottleneck capacity to all edges in the cycle to take care of the negative cycle. Additionally, since it involves demand nodes, the Bellman Ford algorithm is invoked.

Follow the steps below to solve the problem:

- Store the
**capacity of an edge**and the**cost of that edge**in two seperate array. - Given the source node
**S**and sink node**T**, picked edge**p**, demand nodes_{i}**d**and distance between nodes_{a}**dist**, search if it’s possible to have flow from**S**to**T**. - If a flow exists, calculate the distance,
**value = dist + pi – pi[k] – cost[k]**. - Compare the distance values in
**dist[ ]**with**value**and keep updating until**minimum flow**is obtained.

Below is the implementation of the above approach:

## Java

`// Java Program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `public` `class` `MinCostMaxFlow { ` ` ` ` ` `// Stores the found edges ` ` ` `boolean` `found[]; ` ` ` ` ` `// Stores the number of nodes ` ` ` `int` `N; ` ` ` ` ` `// Stores the capacity ` ` ` `// of each edge ` ` ` `int` `cap[][]; ` ` ` ` ` `int` `flow[][]; ` ` ` ` ` `// Stores the cost per ` ` ` `// unit flow of each edge ` ` ` `int` `cost[][]; ` ` ` ` ` `// Stores the distance from each node ` ` ` `// and picked edges for each node ` ` ` `int` `dad[], dist[], pi[]; ` ` ` ` ` `static` `final` `int` `INF ` ` ` `= Integer.MAX_VALUE / ` `2` `- ` `1` `; ` ` ` ` ` `// Function to check if it is possible to ` ` ` `// have a flow from the src to sink ` ` ` `boolean` `search(` `int` `src, ` `int` `sink) ` ` ` `{ ` ` ` ` ` `// Initialise found[] to false ` ` ` `Arrays.fill(found, ` `false` `); ` ` ` ` ` `// Initialise the dist[] to INF ` ` ` `Arrays.fill(dist, INF); ` ` ` ` ` `// Distance from the source node ` ` ` `dist[src] = ` `0` `; ` ` ` ` ` `// Iterate untill src reaches N ` ` ` `while` `(src != N) { ` ` ` ` ` `int` `best = N; ` ` ` `found[src] = ` `true` `; ` ` ` ` ` `for` `(` `int` `k = ` `0` `; k < N; k++) { ` ` ` ` ` `// If already found ` ` ` `if` `(found[k]) ` ` ` `continue` `; ` ` ` ` ` `// Evaluate while flow ` ` ` `// is still in supply ` ` ` `if` `(flow[k][src] != ` `0` `) { ` ` ` ` ` `// Obtain the total value ` ` ` `int` `val ` ` ` `= dist[src] + pi[src] ` ` ` `- pi[k] - cost[k][src]; ` ` ` ` ` `// If dist[k] is > minimum value ` ` ` `if` `(dist[k] > val) { ` ` ` ` ` `// Update ` ` ` `dist[k] = val; ` ` ` `dad[k] = src; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(flow[src][k] < cap[src][k]) { ` ` ` ` ` `int` `val = dist[src] + pi[src] ` ` ` `- pi[k] + cost[src][k]; ` ` ` ` ` `// If dist[k] is > minimum value ` ` ` `if` `(dist[k] > val) { ` ` ` ` ` `// Update ` ` ` `dist[k] = val; ` ` ` `dad[k] = src; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(dist[k] < dist[best]) ` ` ` `best = k; ` ` ` `} ` ` ` ` ` `// Update src to best for ` ` ` `// next iteration ` ` ` `src = best; ` ` ` `} ` ` ` ` ` `for` `(` `int` `k = ` `0` `; k < N; k++) ` ` ` `pi[k] ` ` ` `= Math.min(pi[k] + dist[k], ` ` ` `INF); ` ` ` ` ` `// Return the value obtained at sink ` ` ` `return` `found[sink]; ` ` ` `} ` ` ` ` ` `// Function to obtain the maximum Flow ` ` ` `int` `[] getMaxFlow(` `int` `cap[][], ` `int` `cost[][], ` ` ` `int` `src, ` `int` `sink) ` ` ` `{ ` ` ` ` ` `this` `.cap = cap; ` ` ` `this` `.cost = cost; ` ` ` ` ` `N = cap.length; ` ` ` `found = ` `new` `boolean` `[N]; ` ` ` `flow = ` `new` `int` `[N][N]; ` ` ` `dist = ` `new` `int` `[N + ` `1` `]; ` ` ` `dad = ` `new` `int` `[N]; ` ` ` `pi = ` `new` `int` `[N]; ` ` ` ` ` `int` `totflow = ` `0` `, totcost = ` `0` `; ` ` ` ` ` `// If a path exist from src to sink ` ` ` `while` `(search(src, sink)) { ` ` ` ` ` `// Set the default amount ` ` ` `int` `amt = INF; ` ` ` `for` `(` `int` `x = sink; x != src; x = dad[x]) ` ` ` ` ` `amt = Math.min(amt, ` ` ` `flow[x][dad[x]] != ` `0` ` ` `? flow[x][dad[x]] ` ` ` `: cap[dad[x]][x] ` ` ` `- flow[dad[x]][x]); ` ` ` ` ` `for` `(` `int` `x = sink; x != src; x = dad[x]) { ` ` ` ` ` `if` `(flow[x][dad[x]] != ` `0` `) { ` ` ` `flow[x][dad[x]] -= amt; ` ` ` `totcost -= amt * cost[x][dad[x]]; ` ` ` `} ` ` ` `else` `{ ` ` ` `flow[dad[x]][x] += amt; ` ` ` `totcost += amt * cost[dad[x]][x]; ` ` ` `} ` ` ` `} ` ` ` `totflow += amt; ` ` ` `} ` ` ` ` ` `// Return pair total cost and sink ` ` ` `return` `new` `int` `[] { totflow, totcost }; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `// Creating an object flow ` ` ` `MinCostMaxFlow flow = ` `new` `MinCostMaxFlow(); ` ` ` ` ` `int` `s = ` `0` `, t = ` `4` `; ` ` ` ` ` `int` `cap[][] = { { ` `0` `, ` `3` `, ` `1` `, ` `0` `, ` `3` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `2` `, ` `0` `, ` `0` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `0` `, ` `1` `, ` `6` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `0` `, ` `0` `, ` `2` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `0` `, ` `0` `, ` `0` `} }; ` ` ` ` ` `int` `cost[][] = { { ` `0` `, ` `1` `, ` `0` `, ` `0` `, ` `2` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `0` `, ` `3` `, ` `0` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `0` `, ` `0` `, ` `0` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `0` `, ` `0` `, ` `1` `}, ` ` ` `{ ` `0` `, ` `0` `, ` `0` `, ` `0` `, ` `0` `} }; ` ` ` ` ` `int` `ret[] = flow.getMaxFlow(cap, cost, s, t); ` ` ` ` ` `System.out.println(ret[` `0` `] + ` `" "` `+ ret[` `1` `]); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6 8

**Time Complexity:** O(V^{2} * E^{2}) where V is the number of vertices and E is the number of edges.

**Auxiliary Space: **O(V)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Detect a negative cycle in a Graph | (Bellman Ford)
- Bellman–Ford Algorithm | DP-23
- Bellman Ford Algorithm (Simple Implementation)
- Ford-Fulkerson Algorithm for Maximum Flow Problem
- Dinic's algorithm for Maximum Flow
- Minimum Cost using Dijkstra by reducing cost of an Edge
- Minimum Cost Graph
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Minimize cost to color all the vertices of an Undirected Graph using given operation
- Find minimum s-t cut in a flow network
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Detect cycle in the graph using degrees of nodes of graph
- Minimize cost to color all the vertices of an Undirected Graph
- Detect a negative cycle in a Graph using Shortest Path Faster Algorithm
- Find the minimum cost to reach destination using a train
- Minimum Cost to make all array elements equal using given operations
- Minimize Cash Flow among a given set of friends who have borrowed money from each other

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.