Minimum Weighted Subgraph With the Required Paths - Problem
Minimum Weighted Subgraph With the Required Paths

You're given a weighted directed graph with n nodes (numbered 0 to n-1) and need to find the minimum weight subgraph that allows both src1 and src2 to reach a common destination dest.

The graph is represented by an array edges where edges[i] = [from, to, weight] represents a directed edge from node from to node to with the given weight.

Goal: Find the minimum total weight of edges needed to create paths from both src1 β†’ dest and src2 β†’ dest. The paths can share common edges (which helps minimize the total weight).

Key Insight: The optimal subgraph will likely have paths that converge at some intermediate node before reaching the destination, allowing edge sharing to minimize total weight.

Input & Output

example_1.py β€” Basic convergence case
$ Input: n = 6, edges = [[0,2,2],[0,5,6],[1,2,3],[1,4,5],[2,3,1],[2,4,2],[3,5,1],[4,5,1]], src1 = 0, src2 = 1, dest = 5
β€Ί Output: 9
πŸ’‘ Note: Optimal path: src1(0)β†’2β†’4β†’5 (cost 5) and src2(1)β†’2β†’4β†’5 (cost 6), sharing edge 2β†’4β†’5, total unique edges cost = 2+3+2+1 = 8. Actually, better: 0β†’2 (2) + 1β†’2 (3) + 2β†’3 (1) + 3β†’5 (1) = 7, but paths don't converge. Best is 0β†’2β†’3β†’5 (4) + 1β†’4β†’5 (6) = 10 total weight, or convergence at node 2: 0β†’2 (2) + 1β†’2 (3) + 2β†’4β†’5 (3) = 8.
example_2.py β€” No shared path optimal
$ Input: n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 2, dest = 1
β€Ί Output: 2
πŸ’‘ Note: Both sources go directly to destination: 0β†’1 (cost 1) + 2β†’1 (cost 1) = total cost 2. No benefit from trying to converge paths since they already end at the same destination.
example_3.py β€” Impossible case
$ Input: n = 4, edges = [[0,1,1],[1,2,1]], src1 = 0, src2 = 3, dest = 2
β€Ί Output: -1
πŸ’‘ Note: src2 (node 3) has no outgoing edges, so it's impossible to reach dest (node 2) from src2. Since both sources must be able to reach the destination, return -1.

Visualization

Tap to expand
Warehouse1 (src1)Warehouse2 (src2)DistributionCenter (k)Customer(dest)$5$3$2$8$7πŸ’‘ Optimization StrategyOption 1 - Independent: W1β†’Customer ($8) + W2β†’Customer ($7) = $15Option 2 - Converge: W1β†’DC ($5) + W2β†’DC ($3) + DCβ†’Customer ($2) = $10 βœ“Result: Converging at distribution center saves $5 in shipping costs!
Understanding the Visualization
1
Map all routes from Warehouse 1
Calculate the shortest delivery cost from warehouse 1 to every distribution center
2
Map all routes from Warehouse 2
Calculate the shortest delivery cost from warehouse 2 to every distribution center
3
Map routes to customer
Calculate the shortest delivery cost from every distribution center to the final customer
4
Find optimal strategy
Compare independent deliveries vs. converging at distribution centers and sharing final delivery
Key Takeaway
🎯 Key Insight: The optimal solution considers both independent paths and convergence strategies, using three shortest-path computations to efficiently evaluate all possibilities.

Time & Space Complexity

Time Complexity
⏱️
O(2^n)

In worst case, need to explore exponentially many paths

n
2n
⚠ Quadratic Growth
Space Complexity
O(2^n)

Need to store all possible paths

n
2n
⚠ Quadratic Space

Constraints

  • 3 ≀ n ≀ 105
  • 0 ≀ edges.length ≀ 105
  • edges[i].length == 3
  • 0 ≀ fromi, toi, src1, src2, dest ≀ n - 1
  • fromi β‰  toi
  • src1, src2, and dest are pairwise distinct
  • 1 ≀ weighti ≀ 105
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen