python 10 line Optimized Djikstra solution & 8 line Bellman Ford
Solution 1: Djikstra O(NlogN + len(times)) \npython\nclass Solution:\n    def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:\n        gr
Solution 1: Djikstra O(NlogN + len(times)) \n```python\nclass Solution:\n    def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:\n        graph, seen, heap = collections.defaultdict(dict), set(), [(0, K)]\n        for u, v, w in times:\n            graph[u][v] = w\n        while heap:\n            w, u = heapq.heappop(heap)\n            if u in seen: continue\n            seen, res = seen | {u}, w\n            for v in graph[u]:\n                if v not in seen: heapq.heappush(heap, (w + graph[u][v], v))\n        return res if len(seen) == N else -1\n```\n\nSolution 2: Bellman Ford O(Nlen(times))\n```python\nclass Solution:\n    def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:\n        dp = [0] + [float(\'inf\')] * N\n        dp[K] = 0\n        for _ in range(N):\n            for u, v, w in times:\n                if dp[u] != float(\'inf\') and dp[u] + w < dp[v]:\n                    dp[v] = dp[u] + w\n        res = max(dp or [0])\n        return -1 if res == float(\'inf\') else res\n```
[]