Straightforward Python Dijkstra's
Nothing fancy, just apply Dijkstra's algorithm to the input. The only pitfall is that you have to check that all nodes are reachable from source node, else retu
Nothing fancy, just apply Dijkstra's algorithm to the input. The only pitfall is that you have to check that all nodes are reachable from source node, else return -1.\n\n*- Yangshun*\n\n```\nclass Solution(object):\n    def networkDelayTime(self, times, N, K):\n        from collections import defaultdict\n        nodes = defaultdict(dict)\n        Q = set(range(N))\n        for u, v, w in times:\n            nodes[u - 1][v - 1] = w\n        dist = [float('inf')] * N\n        dist[K - 1] = 0\n        while len(Q):\n            u = None\n            for node in Q:\n                if u == None or dist[node] < dist[u]:\n                    u = node\n            Q.remove(u)\n            for v in nodes[u]:\n                alt = dist[u] + nodes[u][v]\n                if alt < dist[v]:\n                    dist[v] = alt\n        d = max(dist)\n        return -1 if d == float('inf') else d\n```
[]