[Java] Dijkstra's algorithm
I feel like this had very tight limit for Java... even with primitive arrays it barely passes on time (some attempts TLE)\n\nSame code in c++ will comfortably p
I feel like this had very tight limit for Java... even with primitive arrays it barely passes on time (some attempts TLE)\n\nSame code in c++ will comfortably pass\n\n```\n    static int[][][] g;\n    static int[][] edges;\n    static int n;\n\n    public int minCost(int maxTime, int[][] e, int[] passingFees) {\n        edges = e;\n        n = passingFees.length;\n        g = packG();\n        final int[][] dp = new int[n][maxTime + 1];\n        for (int i = 0; i < n; i++) {\n            Arrays.fill(dp[i], (int) 1e9);\n        }\n        dp[0][0] = passingFees[0];\n        final PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));\n        pq.offer(new int[] { passingFees[0], 0, 0 });\n        while (!pq.isEmpty()) {\n            final int[] curr = pq.remove();\n            final int d = curr[0];\n            final int u = curr[1];\n            final int time = curr[2];\n            if (dp[u][time] < d) {\n                continue;\n            }\n            for (int[] next : g[u]) {\n                final int nextTime = time + next[1];\n                if (nextTime <= maxTime) {\n                    if (dp[next[0]][nextTime] > dp[u][time] + passingFees[next[0]]) {\n                        dp[next[0]][nextTime] = dp[u][time] + passingFees[next[0]];\n                        pq.offer(new int[] { dp[next[0]][nextTime], next[0], nextTime });\n                    }\n                }\n            }\n        }\n        int res = (int) 1e9;\n        for (int i = 0; i <= maxTime; i++) {\n            res = Math.min(res, dp[n - 1][i]);\n        }\n        return res == (int) 1e9 ? -1 : res;\n    }\n\n    private static int[][][] packG() {\n        final int[][][] g = new int[n][][];\n        final int[] size = new int[n];\n        for (int[] edge : edges) {\n            ++size[edge[0]];\n            ++size[edge[1]];\n        }\n        for (int i = 0; i < n; i++) {\n            g[i] = new int[size[i]][2];\n        }\n        for (int[] edge : edges) {\n            g[edge[0]][--size[edge[0]]] = new int[] { edge[1], edge[2] };\n            g[edge[1]][--size[edge[1]]] = new int[] { edge[0], edge[2] };\n        }\n        return g;\n    }\n```
[]