Network Delay Time LeetCode solution in Python
1import heapq234class Solution:5def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:67adjlist = [[] for _ in range(n + 1)]89for src, dst, weight in times:10adjlist[src].append((dst, weight))1112visited = [-1] * (n + 1)13captured = [-1] * (n + 1)1415priority_queue = [(0, k)]1617nums = 018total_dist = 01920while priority_queue:2122dst, node = heapq.heappop(priority_queue)2324if captured[node] != -1:25continue26captured[node] = dst2728nums += 129total_dist = dst3031for nbr, wt in adjlist[node]:32if captured[nbr] == -1:33heapq.heappush(priority_queue, (captured[node] + wt, nbr))34if nums == n:35return total_dist36else:37return -1