Minimum Time to Visit Disappearing Nodes - Problem

You're navigating through a time-sensitive network where nodes can vanish at specific moments! Given an undirected graph with n nodes, you need to find the shortest path from node 0 to every other node before they disappear.

The graph is described by:

  • edges[i] = [ui, vi, lengthi]: An edge between nodes ui and vi with traversal time of lengthi units
  • disappear[i]: The time when node i vanishes from the graph (you cannot visit it after this time)

Important: The graph might be disconnected and can contain multiple edges between the same pair of nodes.

Goal: Return an array where answer[i] is the minimum time to reach node i from node 0. If node i is unreachable or disappears before you can reach it, return -1.

Input & Output

example_1.py โ€” Basic Connected Graph
$ Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]
โ€บ Output: [0, -1, 4]
๐Ÿ’ก Note: Node 0 is the starting point (time 0). Node 1 disappears at time 1, but the shortest path takes time 2, so it's unreachable (-1). Node 2 can be reached via path 0โ†’2 in time 4, which is before its disappear time of 5.
example_2.py โ€” Multiple Paths Available
$ Input: n = 4, edges = [[0,1,5],[0,2,3],[1,3,2],[2,3,4]], disappear = [10,8,8,10]
โ€บ Output: [0, 5, 3, 7]
๐Ÿ’ก Note: All nodes are reachable: Node 1 via 0โ†’1 (time 5), Node 2 via 0โ†’2 (time 3), Node 3 via 0โ†’1โ†’3 (time 7, shorter than 0โ†’2โ†’3 which takes time 7).
example_3.py โ€” Disconnected Graph Edge Case
$ Input: n = 3, edges = [[0,1,2]], disappear = [10,10,10]
โ€บ Output: [0, 2, -1]
๐Ÿ’ก Note: Node 2 is completely disconnected from the component containing nodes 0 and 1, so it's unreachable regardless of its disappear time.

Constraints

  • 1 โ‰ค n โ‰ค 5 ร— 104
  • 0 โ‰ค edges.length โ‰ค 2 ร— 105
  • edges[i].length == 3
  • 0 โ‰ค ui, vi โ‰ค n - 1
  • 0 โ‰ค lengthi โ‰ค 104
  • disappear.length == n
  • 1 โ‰ค disappear[i] โ‰ค 104
  • The graph may contain multiple edges and self-loops

Visualization

Tap to expand
HQt=0HeadquartersS1tโ‰ค8Station 1S2tโ‰ค3Station 2S3tโ‰ค15Station 35 units6 unitsToo slow!3 unitsDijkstra's Priority Queue1. (0, HQ) โ† Current2. (5, S1)3. (8, S3)๐Ÿšจ Emergency Network: Find fastest routes before stations close
Understanding the Visualization
1
Setup Network
Build the rescue network with weighted paths and closure times for each station
2
Start from HQ
Begin from headquarters (node 0) at time 0, using a priority queue to explore nearest stations first
3
Check Time Constraints
For each path, verify we can reach the destination before its closure time
4
Find Optimal Routes
Use Dijkstra's algorithm to find shortest valid paths to all reachable stations
Key Takeaway
๐ŸŽฏ Key Insight: Dijkstra's algorithm naturally finds the shortest paths, and by checking disappear times during edge relaxation, we ensure all returned paths are valid and optimal.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
42.3K Views
High Frequency
~25 min Avg. Time
1.9K 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