Imagine you're a delivery driver navigating a city with one-way streets, and you need to find the shortest route to any of your delivery destinations from your current location.
You're given a directed weighted graph representing a city where:
- There are
nintersections (nodes) numbered from0ton-1 - Each edge
[u, v, w]represents a one-way street from intersectionuto intersectionvwith travel timew - You start at intersection
s - You have multiple possible destinations stored in the
markedarray
Your goal: Find the minimum travel time from your starting point s to reach any of the marked destinations. If no destinations are reachable, return -1.
Example: If you're at intersection 0 and need to deliver to intersections [2, 3], find the shortest path to whichever destination is closest.
Input & Output
Visualization
Time & Space Complexity
Single Dijkstra's algorithm with early termination - same as standard shortest path
Space for adjacency list, priority queue, distance array, and marked set
Constraints
- 2 β€ n β€ 500
- 1 β€ edges.length β€ 104
- edges[i].length == 3
- 0 β€ ui, vi β€ n-1
- 1 β€ wi β€ 100
- 1 β€ marked.length β€ n
- All marked nodes are distinct
- 0 β€ s β€ n-1