You have to assign each arc a flow (an integer between 0 and its capacity, inclusive). For every vertex except 1 and n, the total flow on the arcs going to this vertex must be equal to the total flow on the arcs going from that vertex. Let the flow on the i-th arc be fi, then the cost of the flow is equal to . You have to find a flow which minimizes the cost.
Assistant
i=1∑mfiwi
From the problem fragment: You have to assign each arc a flow (an integer between 0 and its capacity, inclusive). For every vertex except 1 and n, the total flow on the arcs going to this vertex must be equal to the total flow on the arcs going from that vertex. Let the flow on the i-th arc be fi, then the cost of the flow is equal to i=1∑mfiwi. You have to find a flow which minimizes the
Key insight: Understanding the Problem
This appears to involve: graph
Working through the approach: Minimum Cost Flow Problem - Solution
Understanding the Problem
This is the Minimum Cost Maximum Flow (MCMF) problem where:
We have a directed graph with n vertices and m arcs
Each arc has a capacity and a cost (weight)
We need to find a flow from vertex 1 (source) to vertex n (sink)
Objective: Minimize total cost ∑i=1
Algorithm: Successive Shortest Path (SSP)
The optimal strategy is to iteratively augment flow along minimum-cost paths in the residual network. SPFA/Bellman-Ford: Used to find shortest paths (handles negative costs in residual graph)
Algorithm Steps
code
1. Find shortest path using SPFA (handles negative edges)
b. Calculate bottleneck capacity along path
c. Update total cost
3. Return (max_flow, min_cost)
Complete Python Implementation
python
from collections import deque
class MinCostMaxFlow:
def __init__(self, n):
self.n = n
self.adj = [[] for _ in range(n)]
def add_edge(self, u, v, cap, cost):
Edge: [to, capacity, cost, reverse_index]
self.adj[u].append([v, cap, cost, len(self.adj[v])])
self.adj[v].append([u, 0, -cost, len(self.adj[u]) - 1])
def solve(self, s, t):
n, adj = self.n, self.adj
INF = 10*18
total_flow = 0
total_cost = 0
while True:
SPFA to find shortest path
dist = [INF] n
dist[s] = 0
parent = [-1] n
parent_edge = [-1] n
in_queue = [False] n
q = deque([s])
in_queue[s] = True
while q:
u = q.popleft()
in_queue[u] = False
for i, (v, cap, cost, _) in enumerate(adj[u]):
if cap > 0 and dist[u] + cost < dist[v]:
dist[v] = dist[u] + cost
parent[v] = u
parent_edge[v] = i
if not in_queue[v]:
q.append(v)
in_queue[v] = True
No augmenting path exists
if dist[t] == INF:
break
Find bottleneck capacity
path_cap = INF
v = t
while v != s:
u = parent[v]
edge_idx = parent_edge[v]
path_cap = min(path_cap, adj[u][edge_idx][1])
v = u
Augment flow
v = t
while v != s:
u = parent[v]
edge_idx = parent_edge[v]
adj[u][edge_idx][1] -= path_cap Reduce forward capacity
rev_idx = adj[u][edge_idx][3]
adj[v][rev_idx][1] += path_cap Increase reverse capacity
v = u
total_flow += path_cap
total_cost += path_cap dist[t]
return total_flow, total_cost
Example usage:
mcmf = MinCostMaxFlow(n)
mcmf.add_edge(u, v, capacity, cost)
max_flow, min_cost = mcmf.solve(0, n-1) source=0, sink=n-1
Complexity Analysis
| Aspect | Complexity |
Time | O(F⋅V⋅E) with SPFA |
| Space | O(V+E) |
Where F = maximum flow, V = vertices, E = edges
Correctness Argument
Termination: Capacities are finite, algorithm terminates when no path exists
Integer flow: Since all capacities are integers, flows remain integers
Verification Example
For a simple graph: 1 → 2 → 3 with capacities [5, 3] and costs [2, 1]:
Path 1→2→3 has cost 2+1=3 per unit flow
Max flow = min(5, 3) = 3
Min cost = 3 × 3 = 9
The algorithm finds this by augmenting 3 units along the single available path.
I should verify this approach against the given examples to make sure it works.
Minimum Cost Flow Problem - Solution
Understanding the Problem
This is the Minimum Cost Maximum Flow (MCMF) problem where:
We have a directed graph with n vertices and m arcs
Each arc has a capacity and a cost (weight)
We need to find a flow from vertex 1 (source) to vertex n (sink)
Objective: Minimize total cost ∑i=1
Algorithm: Successive Shortest Path (SSP)
The optimal strategy is to iteratively augment flow along minimum-cost paths in the residual network.
Key Concepts
Residual Graph: For each arc with flow f and capacity c:
Forward edge: capacity c−f, cost w
Backward edge: capacity f, cost
Algorithm Steps
code
1. Initialize: flow = 0 for all edges
2. While path exists from source to sink:
a. Find shortest path using SPFA (handles negative edges)
b. Calculate bottleneck capacity along path
c. Augment flow along path
d. Update total cost
3. Return (max_flow, min_cost)
Complete Python Implementation
python
from collections import deque
class MinCostMaxFlow:
def __init__(self, n):
self.n = n
self.adj = [[] for _ in range(n)]
def add_edge(self, u, v, cap, cost):
# Edge: [to, capacity, cost, reverse_index]
self.adj[u].append([v, cap, cost, len(self.adj[v])])
self.adj[v].append([u, 0, -cost, len(self.adj[u]) - 1])
def solve(self, s, t):
n, adj = self.n, self.adj
INF = 10**18
total_flow = 0
total_cost = 0
while True:
# SPFA to find shortest path
dist = [INF] * n
dist[s] = 0
parent = [-1] * n
parent_edge = [-1] * n
in_queue = [False] * n
q = deque([s])
in_queue[s] = True
while q:
u = q.popleft()
in_queue[u] = False
for i, (v, cap, cost, _) in enumerate(adj[u]):
if cap > 0 and dist[u] + cost < dist[v]:
dist[v] = dist[u] + cost
parent[v] = u
parent_edge[v] = i
if not in_queue[v]:
q.append(v)
in_queue[v] = True
# No augmenting path exists
if dist[t] == INF:
break
# Find bottleneck capacity
path_cap = INF
v = t
while v != s:
u = parent[v]
edge_idx = parent_edge[v]
path_cap = min(path_cap, adj[u][edge_idx][1])
v = u
# Augment flow
v = t
while v != s:
u = parent[v]
edge_idx = parent_edge[v]
adj[u][edge_idx][1] -= path_cap # Reduce forward capacity
rev_idx = adj[u][edge_idx][3]
adj[v][rev_idx][1] += path_cap # Increase reverse capacity
v = u
total_flow += path_cap
total_cost += path_cap * dist[t]
return total_flow, total_cost
# Example usage:
# mcmf = MinCostMaxFlow(n)
# mcmf.add_edge(u, v, capacity, cost)
# max_flow, min_cost = mcmf.solve(0, n-1) # source=0, sink=n-1
Complexity Analysis
Aspect
Complexity
Time
O(F⋅V⋅E) with SPFA
Space
O(V+E)
Where F = maximum flow, V = vertices, E = edges
Correctness Argument
Optimality: Each augmentation uses the cheapest available path
Residual edges allow "undoing" suboptimal choices
Termination: Capacities are finite, algorithm terminates when no path exists
Integer flow: Since all capacities are integers, flows remain integers
Verification Example
For a simple graph: 1 → 2 → 3 with capacities [5, 3] and costs [2, 1]:
Path 1→2→3 has cost 2+1=3 per unit flow
Max flow = min(5, 3) = 3
Min cost = 3 × 3 = 9
The algorithm finds this by augmenting 3 units along the single available path.
m
fi
wi
m
fi
wi
−w
Optimal Substructure: Augmenting along shortest paths ensures minimum cost
SPFA/Bellman-Ford: Used to find shortest paths (handles negative costs in residual graph)