The Minimum Weighted Subgraph With the Required Paths problem gives you a directed weighted graph and three special nodes: src1, src2, and dest. Find the minimum weight subgraph such that there exist paths from src1 to dest and from src2 to dest. This means finding paths from both sources that share a common "meeting point" on the way to dest. This hard coding problem requires three Dijkstra runs.
Google asks this because it requires the non-obvious insight that the optimal solution always has a "merge point" — a node where the path from src1 and the path from src2 converge before continuing to dest. By running Dijkstra from src1, src2, and from dest on the reversed graph, you can efficiently find this optimal merge point. The shortest path, graph, and heap interview pattern is fully exercised.
Three Dijkstra runs.
dist1[v] = shortest path from src1 to v.dist2[v] = shortest path from src2 to v.distDest[v] = shortest path from v to dest.For each node v, compute dist1[v] + dist2[v] + distDest[v]. The minimum over all v is the answer. Return -1 if no valid subgraph exists.
Graph: src1=0, src2=1, dest=5. Edges: (0→2, 2), (1→2, 3), (2→3, 1), (2→4, 2), (3→5, 4), (4→5, 1).
"Two sources to one destination" problems always have a meeting point where the two paths merge. The three-Dijkstra technique — from each source forward and from the destination backward — is the canonical solution. Practice building reversed graphs (flip all edge directions) and running Dijkstra on them. This pattern generalizes to any problem involving "optimal meeting point for multiple travelers" on weighted graphs and is a recurring hard problem at Google and Meta.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Modify Graph Edge Weights | Hard | Solve | |
| Reachable Nodes In Subdivided Graph | Hard | Solve | |
| Find Shortest Path with K Hops | Hard | Solve | |
| Minimum Cost Path with Edge Reversals | Medium | Solve | |
| Minimum Cost to Reach City With Discounts | Medium | Solve |