The Minimum Cost Path with Edge Reversals problem gives you a directed graph. You need to find a path from a source to a destination. However, the graph might not have a direct path. You are allowed to "reverse" any existing edge, but each reversal has a cost of 1. You want to find the path that requires the minimum number of edge reversals.
This is a classic Minimum Cost Path interview question at Meta and Palantir. It tests a candidate's ability to transform a graph problem into a Shortest Path problem. It evaluates if you can think creatively about "edge weights" by assigning a cost of 0 to original edges and a cost of 1 to reversed versions of those edges.
The 0-1 BFS or Dijkstra's Algorithm interview pattern is the core here.
(u, v), add it to a new graph with weight 0.(v, u) with weight 1.Graph: 0 -> 1, 1 -> 2, 2 <- 3. Find path 0 to 3.
0 -> 1 -> 2 costs 0.2 to 3, there is no original edge, but there is 3 -> 2.3 -> 2 to get 2 -> 3 with cost 1.0 + 0 + 1 = 1.
Only one edge reversal is needed.When you encounter a problem where "some moves are free and some have a unit cost," think of 0-1 BFS. It's a powerful optimization of BFS using a deque where 0-cost moves go to the front and 1-cost moves go to the back.