There is an undirected graph of n nodes. You are given a 2D array edges, where edges[i] = [ui, vi, lengthi] describes an edge between node ui and node vi with a traversal time of lengthi units.
Additionally, you are given an array disappear, where disappear[i] denotes the time when the node i disappears from the graph and you won't be able to visit it.
Note that the graph might be disconnected and might contain multiple edges.
Return the array answer, with answer[i] denoting the minimum units of time required to reach node i from node 0. If node i is unreachable from node 0 then answer[i] is -1.
Example 1:
Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]
Output: [0,-1,4]
Explanation:

We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.
edges[0]. Unfortunately, it disappears at that moment, so we won't be able to visit it.edges[2].Example 2:
Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]
Output: [0,2,3]
Explanation:

We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.
edges[0].edges[0] and edges[1].Example 3:
Input: n = 2, edges = [[0,1,1]], disappear = [1,1]
Output: [0,-1]
Explanation:
Exactly when we reach node 1, it disappears.
Constraints:
1 <= n <= 5 * 1040 <= edges.length <= 105edges[i] == [ui, vi, lengthi]0 <= ui, vi <= n - 11 <= lengthi <= 105disappear.length == n1 <= disappear[i] <= 105Dijkstra's Algorithm with Constraints:
Use Dijkstra's algorithm to find the shortest paths from node 0 to all other nodes. However, during the traversal, consider the disappearance times of the nodes. If a node is reached after its disappearance time, consider it unreachable for that path.
This solution utilizes a modified version of Dijkstra's algorithm. The algorithm maintains a priority queue to ensure processing nodes with the least cumulative time first. Every time a node is dequeued, it checks if it was reached before its disappearance time. If not, it's effectively unreachable. This ensures all nodes that can be visited in time are correctly calculated.
Java
Time Complexity: O((n + e) log n), where n is the number of nodes and e is the number of edges.
Space Complexity: O(n + e), to store the graph and auxiliary data.
Breadth-First Search with Edge Constraints:
Employ a breadth-first search (BFS) to explore the graph from the starting node. Use a queue to manage nodes to explore and ensure to update the shortest time to reach each node. Similar to Dijkstra, ensure that any node visited must be checked against its disappearance time to ensure the path is valid.
This C++ solution uses a BFS approach to explore the graph. A simple queue is used instead of a priority queue, but we must still ensure that paths followed are viable by checking the disappear quality of nodes. Similar to BFS, this approach traverses layer by layer in time when optimizing reachability.
C#
Time Complexity: O(n + e), because each node and edge is processed once.
Space Complexity: O(n + e), to store nodes and other state data.
| Approach | Complexity |
|---|---|
| Dijkstra's Algorithm with Constraints | Time Complexity: O((n + e) log n), where n is the number of nodes and e is the number of edges. |
| Breadth-First Search with Edge Constraints | Time Complexity: O(n + e), because each node and edge is processed once. |
I solved too many Leetcode problems • NeetCodeIO • 101,495 views views
Watch 9 more video solutions →Practice Minimum Time to Visit Disappearing Nodes with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor