Python - Dijkstra
py\n\nclass Solution:\n    def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:\n        \n\t\t# build graph\n        g = collections.defa
```py\n\nclass Solution:\n    def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:\n        \n\t\t# build graph\n        g = collections.defaultdict(list)\n        for u, v, cost in times:\n            g[u].append((cost,v))\n        \n        min_heap = [(0, K)] # cost, u\n        visited = set()\n        distance = {i:float(\'inf\') for i in range(1, N+1)}\n        distance[K] = 0\n        while min_heap:\n            cost, u = heapq.heappop(min_heap)\n            if u in visited:\n                continue\n            visited.add(u)\n            if len(visited) == N: # Found!\n                return cost\n            \n            for cost2, v in g[u]:\n                if cost + cost2 < distance[v] and v not in visited:\n                    distance[v] = cost + cost2\n                    heapq.heappush(min_heap, (cost + cost2, v))\n        return -1\n            \n```
[]