結果

問題 No.1 道のショートカット
ユーザー tentententen
提出日時 2020-10-22 16:46:21
言語 Java19
(openjdk 21)
結果
AC  
実行時間 408 ms / 5,000 ms
コード長 2,488 bytes
コンパイル時間 3,545 ms
コンパイル使用メモリ 75,952 KB
実行使用メモリ 61,356 KB
最終ジャッジ日時 2023-09-28 14:38:46
合計ジャッジ時間 15,893 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,996 KB
testcase_01 AC 129 ms
55,416 KB
testcase_02 AC 129 ms
55,716 KB
testcase_03 AC 136 ms
55,940 KB
testcase_04 AC 130 ms
56,104 KB
testcase_05 AC 128 ms
55,680 KB
testcase_06 AC 141 ms
55,500 KB
testcase_07 AC 143 ms
55,980 KB
testcase_08 AC 275 ms
61,004 KB
testcase_09 AC 219 ms
59,292 KB
testcase_10 AC 255 ms
59,604 KB
testcase_11 AC 311 ms
60,888 KB
testcase_12 AC 402 ms
61,356 KB
testcase_13 AC 403 ms
61,340 KB
testcase_14 AC 171 ms
55,500 KB
testcase_15 AC 127 ms
55,944 KB
testcase_16 AC 197 ms
56,952 KB
testcase_17 AC 128 ms
56,120 KB
testcase_18 AC 182 ms
56,572 KB
testcase_19 AC 156 ms
55,776 KB
testcase_20 AC 212 ms
58,804 KB
testcase_21 AC 192 ms
55,900 KB
testcase_22 AC 157 ms
56,020 KB
testcase_23 AC 375 ms
61,308 KB
testcase_24 AC 377 ms
61,088 KB
testcase_25 AC 228 ms
58,464 KB
testcase_26 AC 206 ms
56,764 KB
testcase_27 AC 348 ms
60,976 KB
testcase_28 AC 134 ms
56,080 KB
testcase_29 AC 279 ms
60,772 KB
testcase_30 AC 206 ms
58,636 KB
testcase_31 AC 252 ms
59,824 KB
testcase_32 AC 297 ms
61,036 KB
testcase_33 AC 237 ms
59,300 KB
testcase_34 AC 408 ms
61,128 KB
testcase_35 AC 212 ms
58,800 KB
testcase_36 AC 234 ms
59,000 KB
testcase_37 AC 219 ms
59,444 KB
testcase_38 AC 152 ms
58,108 KB
testcase_39 AC 190 ms
56,576 KB
testcase_40 AC 201 ms
60,664 KB
testcase_41 AC 186 ms
57,108 KB
testcase_42 AC 127 ms
55,864 KB
testcase_43 AC 126 ms
55,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int c = sc.nextInt();
        int v = sc.nextInt();
        int[] starts = new int[v];
        for (int i = 0; i < v; i++) {
            starts[i] = sc.nextInt() - 1;
        }
        int[] terminals = new int[v];
        for (int i = 0; i < v; i++) {
            terminals[i] = sc.nextInt() - 1;
        }
        int[] yen = new int[v];
        for (int i = 0; i < v; i++) {
            yen[i] = sc.nextInt();
        }
        int[] mins = new int[v];
        for (int i = 0; i < v; i++) {
            mins[i] = sc.nextInt();
        }
        ArrayList<ArrayList<Route>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < v; i++) {
            graph.get(starts[i]).add(new Route(terminals[i], yen[i], mins[i]));
        }
        int[][] costs = new int[n][c + 1];
        for (int[] arr : costs) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0, c));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (p.yen < 0 || costs[p.idx][p.yen] <= p.value) {
                continue;
            }
            costs[p.idx][p.yen] = p.value;
            for (Route r : graph.get(p.idx)) {
                queue.add(new Path(r.idx, p.value + r.value, p.yen - r.yen));
            }
        }
        int ans = Integer.MAX_VALUE;
        for (int x : costs[n - 1]) {
            ans = Math.min(ans, x);
        }
        if (ans == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        int yen;
        
        public Path(int idx, int value, int yen) {
            this.idx = idx;
            this.value = value;
            this.yen = yen;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
    
    static class Route {
        int idx;
        int yen;
        int value;
        
        public Route(int idx, int yen, int value) {
            this.idx = idx;
            this.yen = yen;
            this.value = value;
        }
    }
}
0