Minimum Weighted Subgraph With the Required Paths - Problem
Minimum Weighted Subgraph With the Required Paths
You're given a weighted directed graph with
The graph is represented by an array
Goal: Find the minimum total weight of edges needed to create paths from both
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.
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
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
β Quadratic Growth
Space Complexity
O(2^n)
Need to store all possible paths
β 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
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code