Java, Djikstra/bfs, Concise and very easy to understand
I think bfs and djikstra are very similar problems. It\'s just that djikstra cost is different compared with bfs, so use priorityQueue instead a Queue for a sta
I think bfs and djikstra are very similar problems. It\'s just that djikstra cost is different compared with bfs, so use priorityQueue instead a Queue for a standard bfs search.\n```\nclass Solution {\n    public int networkDelayTime(int[][] times, int N, int K) {\n        Map<Integer, Map<Integer,Integer>> map = new HashMap<>();\n        for(int[] time : times){\n            map.putIfAbsent(time[0], new HashMap<>());\n            map.get(time[0]).put(time[1], time[2]);\n        }\n        \n        //distance, node into pq\n        Queue<int[]> pq = new PriorityQueue<>((a,b) -> (a[0] - b[0]));\n        \n        pq.add(new int[]{0, K});\n        \n        boolean[] visited = new boolean[N+1];\n        int res = 0;\n        \n        while(!pq.isEmpty()){\n            int[] cur = pq.remove();\n            int curNode = cur[1];\n            int curDist = cur[0];\n            if(visited[curNode]) continue;\n            visited[curNode] = true;\n            res = curDist;\n            N--;\n            if(map.containsKey(curNode)){\n                for(int next : map.get(curNode).keySet()){\n                    pq.add(new int[]{curDist + map.get(curNode).get(next), next});\n                }\n            }\n        }\n        return N == 0 ? res : -1;\n            \n    }\n}\n```
[]