VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-time-required-to-visit-each-disappearing-nodes/

⇱ Minimum Time Required to Visit Each Disappearing Nodes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Time Required to Visit Each Disappearing Nodes

Last Updated : 23 Jul, 2025

Given an undirected graph with n nodes that are represented by a 2D array edges where edges[i] = [ui, vi, timei] describes an edge between nodes ui and vi with a traversal time of timei. Additionally, you are given an array disappear[], where disappear[i] denotes the time when node i becomes unavailable. The graph may be disconnected and contain multiple edges. The task is to return an array answer[] where answer[i] represents the minimum time required to reach node i from node 0. If node i is unreachable from node 0, answer[i] should be -1.

Example:

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.

  • For node 0, we don't need any time as it is our starting point.
  • For node 1, we need at least 2 units of time to traverse edges[0]. Unfortunately, it disappears at that moment, so we won't be able to visit it.
  • For node 2, we need at least 4 units of time to traverse edges[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.

  • For node 0, we don't need any time as it is the starting point.
  • For node 1, we need at least 2 units of time to traverse edges[0].
  • For node 2, we need at least 3 units of time to traverse edges[0] and edges[1].

Approach:

We can use Dijkstra's algorithm, which is specifically designed for finding the shortest paths in graphs with non-negative weights. Here's the plan:

We'll represent the graph using an adjacency list, where each node points to its neighbors along with the travel time between them. Dijkstra's algorithm uses a priority queue (min-heap) to ensure we always expand the node with the smallest known distance first, which guarantees that we explore the shortest path first. We'll maintain an array to store the shortest distances to each node, initializing all distances to infinity (except for the source node, which starts at 0). As we process each node, we'll update the distances to its neighboring nodes only if the new distance is shorter and within the time limit before the neighbor disappears. If a node cannot be reached before it disappears, it won't be considered further. After processing all nodes, any node that remains with a distance of infinity means it's unreachable, and we'll set its distance to -1. This way, we ensure to maintain both the shortest paths and the disappearance constraints. Finally, we'll return the array of minimum times required to reach each node.

Steps-by-step approach:

  • Build an adjacency list from the given edges where each node points to its neighbors and the respective traversal times.
  • Use a priority queue (min-heap) to manage nodes by their current known shortest distance.
  • Initialize a distance[] array with all values set to infinity (INT_MAX), except for the source node (node 0), which is set to 0.
  • While the priority queue is not empty:
    • Extract the node with the smallest known distance.
    • For each neighboring node, calculate the potential new distance.
      • If this new distance is shorter than the current known distance and is less than the disappearance time of the neighboring node
        • Update the distance and add the neighbor to the priority queue.
  • Convert all distances that remain infinity (INT_MAX) to -1, indicating those nodes are unreachable from the source node.
  • Return the distance array containing the minimum time required to reach each node from node 0.

Below is the implementation of the above approach:


Output
0 -1 4 

Time Complexity: O(V logV + E), where V is the number of vertices and E is the number of edges.
Auxiliary Space: O(V+E), where V is the number of vertices and E is the number of edges.

Comment