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.
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]`.
We need to find the shortest time to each node, but can only visit a node before its disappear time. This is a classic shortest path problem with an extra constraint, so we use Dijkstra’s algorithm and skip nodes that disappear before we arrive.
classSolution {
publicint[]minimumTime(int n, int[][] edges, int[] disappear) {
List<int[]>[] g =new List[n];
for (int i = 0; i < n; i++) g[i]=new ArrayList<>();
for (int[] e : edges) {
g[e[0]].add(newint[]{e[1], e[2]});
g[e[1]].add(newint[]{e[0], e[2]});
}
int[] ans =newint[n];
Arrays.fill(ans, -1);
PriorityQueue<int[]> pq =new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
pq.offer(newint[]{0, 0});
while (!pq.isEmpty()) {
int[] cur = pq.poll();
int t = cur[0], u = cur[1];
if (ans[u]!=-1 || t >= disappear[u]) continue;
ans[u]= t;
for (int[] e : g[u]) {
int v = e[0], w = e[1];
if (ans[v]==-1) pq.offer(newint[]{t + w, v});
}
}
return ans;
}
}
classSolution {
funminimumTime(n: Int, edges: Array<IntArray>, disappear: IntArray): IntArray {
val g = Array(n) { mutableListOf<Pair<Int, Int>>() }
for (e in edges) {
g[e[0]].add(e[1] to e[2])
g[e[1]].add(e[0] to e[2])
}
val ans = IntArray(n) { -1 }
val pq = PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
pq.add(0 to 0)
while (pq.isNotEmpty()) {
val(t, u) = pq.poll()
if (ans[u] != -1|| t >= disappear[u]) continue ans[u] = t
for ((v, w) in g[u]) {
if (ans[v] == -1) pq.add(t + w to v)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from typing import List
import heapq
classSolution:
defminimumTime(self, n: int, edges: List[List[int]], disappear: List[int]) -> List[int]:
g = [[] for _ in range(n)]
for u, v, w in edges:
g[u].append((v, w))
g[v].append((u, w))
ans = [-1] * n
heap = [(0, 0)]
while heap:
t, u = heapq.heappop(heap)
if ans[u] !=-1or t >= disappear[u]:
continue ans[u] = t
for v, w in g[u]:
if ans[v] ==-1:
heapq.heappush(heap, (t + w, v))
return ans