input: from typing import List
  from heapq import heappop, heappush
  
  def shortest_reach_time(n: int, edges: List[List[int]], disappear: List[int]) -> List[int]:
      """
      You have an undirected graph with n nodes. The 2D array edges is provided where edges[i] = [ui, vi, lengthi] indicates an edge connecting node ui to node vi with a traversal time of lengthi units.
  
      Additionally, there's an array disappear which indicates the time at which each node disappears from the graph, making it unreachable afterward.
  
      Note that the graph might have multiple disconnected components and can include parallel edges.
  
      Return an array result where result[i] represents the minimum time needed to reach node i from node 0. If node i cannot be reached from node 0, result[i] should be -1.
  
      Examples:
      >>> shortest_reach_time(3, [[0,1,2],[1,2,1],[0,2,4]], [1,1,5])
      [0,-1,4]
      >>> shortest_reach_time(3, [[0,1,2],[1,2,1],[0,2,4]], [1,3,5])
      [0,2,3]
      >>> shortest_reach_time(2, [[0,1,1]], [1,1])
      [0,-1]
  
      Constraints:
      1 <= n <= 5 * 10^4
      0 <= edges.length <= 10^5
      edges[i] == [ui, vi, lengthi]
      0 <= ui, vi <= n - 1
      1 <= lengthi <= 10^5
      disappear.length == n
      1 <= disappear[i] <= 10^5
      """
output:     import heapq
  
      # Initialize the graph adjacency list and the disappearance times
      graph = [[] for _ in range(n)]
      for u, v, w in edges:
          graph[u].append((v, w))
          graph[v].append((u, w))
  
      # Dijkstra's algorithm using a priority queue
      pq = [(0, 0)]  # (cost, node)
      min_time = [float('inf')] * n
      min_time[0] = 0
      visited = [False] * n
  
      while pq:
          current_time, u = heapq.heappop(pq)
          if visited[u] or current_time >= disappear[u]:
              continue
  
          visited[u] = True
  
          for v, length in graph[u]:
              if visited[v] or current_time + length >= disappear[v]:
                  continue
  
              new_time = current_time + length
              if new_time < min_time[v]:
                  min_time[v] = new_time
                  heapq.heappush(pq, (new_time, v))
  
      # Replace unreachable nodes' times with -1
      return [time if time != float('inf') else -1 for time in min_time]