Java || Dijkstra || PriorityQueue (Heap) || Fast
An leetcode a day, doctors are always on the way.\n\n\n\nclass Solution {\n    public int networkDelayTime(int[][] times, int n, int k) {\n        \n        //R
An leetcode a day, doctors are always on the way.\n\n```\n\nclass Solution {\n    public int networkDelayTime(int[][] times, int n, int k) {\n        \n        //Represente the graph using adjacency list\n        List<int[]>[] graph = new List[n + 1];\n        for(int i = 0; i <= n; i++){\n            graph[i] = new ArrayList<int[]>();\n        }\n        for(int[] time : times){\n            int from = time[0];\n            int to = time[1];\n            int value = time[2];\n            graph[from].add(new int[]{to, value});\n        }\n        \n        //the distance array\n        int[] dist = new int[n + 1];\n\t\t\n\t\t//a big enough number which the distance will never reach out\n        Arrays.fill(dist, 50000);\n        dist[k] = 0;\n        \n        //non-decending order\n        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);\n        pq.offer(new int[]{0, k});\n        while(!pq.isEmpty()){\n            int[] curr = pq.poll();\n            int time = curr[0];\n            int currNode = curr[1];\n            if(dist[currNode] < time){\n                continue;\n            }\n            for(int[] edge : graph[currNode]){\n                int toNode = edge[0];\n                int distance = edge[1] + dist[currNode];\n                if(distance < dist[toNode]){\n                    dist[toNode] = distance;\n                    pq.offer(new int[]{distance, toNode});\n                }\n            }\n        }\n        int ans = 0;\n\t\t\n\t\t//remember it should be 1\n        for(int i = 1; i < n + 1; i++){\n            ans = Math.max(ans, dist[i]);\n        }\n        return ans == 50000 ? -1 : ans;\n    }\n}\n\n```
['Java']