You are given a directed graph with `N` nodes and `M` edges. Each edge `(u, v)` has an associated maximum flow capacity `C(u, v)` and a cost `W(u, v)` for each unit of flow sent through it. Your task is to find the minimum total cost to send exactly `F` units of flow from a designated source node `S` to a designated sink node `T`. The total cost of sending flow through an edge is `flow_sent * W(u, v)`. The flow must adhere to standard network flow rules: 1. Flow conservation: For any intermediate node (not S or T), the total flow entering the node must equal the total flow leaving it. 2. Capacity constraint: The flow through any edge `(u, v)` cannot exceed its capacity `C(u, v)`. 3. All `F` units of flow must successfully reach the sink node `T` from the source node `S`. If it's impossible to send exactly `F` units of flow from `S` to `T`, return -1.
Explanation: Send 1 unit via path 0->1 (using the edge with cost 2) -> 2 for a total cost of 1*(2+1) = 3. Remaining flow needed is 1 unit. Send the remaining 1 unit via path 0->1 (using the edge with cost 5) -> 2 for a total cost of 1*(5+1) = 6. Total cost is 3 + 6 = 9.
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak