Skip to main content

20240718 3112 - Medium - Dijstra

3112. Minimum Time to Visit Disappearing Nodes

class Solution:
def minimumTime(self, n: int, edges: List[List[int]], disappear: List[int]) -> List[int]:
g = [[] for _ in range(n)] # 稀疏图用邻接表
for x, y, wt in edges:
g[x].append((y, wt))
g[y].append((x, wt))

dis = [-1] * n
dis[0] = 0
h = [(0, 0)]
while h:
dx, x = heappop(h)
if dx > dis[x]: # x 之前出堆过
continue
for y, wt in g[x]:
new_dis = dx + wt
if new_dis < disappear[y] and (dis[y] < 0 or new_dis < dis[y]):
dis[y] = new_dis # 更新 x 的邻居的最短路
heappush(h, (new_dis, y))
return dis