Watch 10 video solutions for Minimum Cost Path with Edge Reversals, a medium level problem involving Graph, Heap (Priority Queue), Shortest Path. This walkthrough by codestorywithMIK has 9,587 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a directed, weighted graph with n nodes labeled from 0 to n - 1, and an array edges where edges[i] = [ui, vi, wi] represents a directed edge from node ui to node vi with cost wi.
Each node ui has a switch that can be used at most once: when you arrive at ui and have not yet used its switch, you may activate it on one of its incoming edges vi → ui reverse that edge to ui → vi and immediately traverse it.
The reversal is only valid for that single move, and using a reversed edge costs 2 * wi.
Return the minimum total cost to travel from node 0 to node n - 1. If it is not possible, return -1.
Example 1:
Input: n = 4, edges = [[0,1,3],[3,1,1],[2,3,4],[0,2,2]]
Output: 5
Explanation:

0 → 1 (cost 3).3 → 1 into 1 → 3 and traverse it at cost 2 * 1 = 2.3 + 2 = 5.Example 2:
Input: n = 4, edges = [[0,2,1],[2,1,1],[1,3,1],[2,3,3]]
Output: 3
Explanation:
0 → 2 (cost 1), then 2 → 1 (cost 1), then 1 → 3 (cost 1).1 + 1 + 1 = 3.
Constraints:
2 <= n <= 5 * 1041 <= edges.length <= 105edges[i] = [ui, vi, wi]0 <= ui, vi <= n - 11 <= wi <= 1000Problem Overview: You are given a directed graph and want the cheapest way to travel from a source node to a destination. Moving along an existing edge costs 0, but reversing an edge direction costs 1. The task is to compute the minimum total cost required to reach the target node.
Approach 1: DFS/Brute Force Exploration (Exponential Time, O(V) space)
A straightforward idea is to explore all possible paths from the source. For each node, traverse both the original outgoing edges (cost 0) and any incoming edges treated as reversed edges (cost 1). Track the accumulated cost and keep the minimum seen when reaching the destination. This approach effectively searches the entire state space of paths. Because graphs may contain cycles and multiple branching choices, the time complexity becomes exponential in the worst case. It mainly helps reason about the problem but is impractical for large graphs.
Approach 2: 0-1 BFS with Deque (O(V + E) time, O(V + E) space)
The key observation is that every edge weight is either 0 or 1. Build an augmented graph where the original edge u → v has weight 0, and the reverse edge v → u has weight 1. Instead of a priority queue, use a deque. When relaxing an edge with cost 0, push the node to the front; when the cost is 1, push it to the back. This keeps nodes ordered by shortest distance without a heap. The algorithm behaves like BFS but respects edge weights, producing optimal shortest paths efficiently. This method is common in graph problems where weights are limited to two values.
Approach 3: Dijkstra's Algorithm with Min Heap (O((V + E) log V) time, O(V + E) space)
Treat the problem as a standard shortest path computation. Construct an adjacency list where each original edge has weight 0 and its reversed counterpart has weight 1. Run Dijkstra's algorithm starting from the source node using a priority queue. Each time you pop the smallest distance node, relax its neighbors and update distances if a cheaper path is found. The heap guarantees the next processed node always has the minimum current cost. This approach works for any non‑negative weights and fits naturally into the shortest path family of problems.
Recommended for interviews: Interviewers typically expect the shortest path formulation. Showing the graph transformation (adding reversed edges with cost 1) demonstrates strong modeling skills. Implementing Dijkstra with a priority queue is the safest general solution, while mentioning the 0-1 BFS optimization shows deeper understanding of graph edge-weight constraints.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS / Brute Force Path Search | Exponential | O(V) | Conceptual understanding or very small graphs |
| 0-1 BFS using Deque | O(V + E) | O(V + E) | Best when edge weights are only 0 or 1 |
| Dijkstra's Algorithm with Min Heap | O((V + E) log V) | O(V + E) | General shortest path solution using priority queue |