結果

問題 No.1 道のショートカット
ユーザー DAZYDAZY
提出日時 2017-05-15 10:47:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 293 ms / 5,000 ms
コード長 2,162 bytes
コンパイル時間 3,605 ms
コンパイル使用メモリ 74,260 KB
実行使用メモリ 60,500 KB
最終ジャッジ日時 2023-09-27 22:06:13
合計ジャッジ時間 12,805 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
55,688 KB
testcase_01 AC 105 ms
55,804 KB
testcase_02 AC 106 ms
56,044 KB
testcase_03 AC 111 ms
53,984 KB
testcase_04 AC 108 ms
56,176 KB
testcase_05 AC 108 ms
55,908 KB
testcase_06 AC 111 ms
56,052 KB
testcase_07 AC 116 ms
55,748 KB
testcase_08 AC 251 ms
60,048 KB
testcase_09 AC 184 ms
58,880 KB
testcase_10 AC 207 ms
59,620 KB
testcase_11 AC 233 ms
59,728 KB
testcase_12 AC 293 ms
60,500 KB
testcase_13 AC 266 ms
60,088 KB
testcase_14 AC 133 ms
55,792 KB
testcase_15 AC 108 ms
55,780 KB
testcase_16 AC 168 ms
56,580 KB
testcase_17 AC 110 ms
56,144 KB
testcase_18 AC 135 ms
56,144 KB
testcase_19 AC 150 ms
55,752 KB
testcase_20 AC 171 ms
56,828 KB
testcase_21 AC 155 ms
55,952 KB
testcase_22 AC 136 ms
55,600 KB
testcase_23 AC 232 ms
59,896 KB
testcase_24 AC 225 ms
58,336 KB
testcase_25 AC 175 ms
56,476 KB
testcase_26 AC 167 ms
56,980 KB
testcase_27 AC 269 ms
60,056 KB
testcase_28 AC 137 ms
55,896 KB
testcase_29 AC 204 ms
59,208 KB
testcase_30 AC 166 ms
58,496 KB
testcase_31 AC 182 ms
58,876 KB
testcase_32 AC 204 ms
58,728 KB
testcase_33 AC 186 ms
58,632 KB
testcase_34 AC 226 ms
60,012 KB
testcase_35 AC 180 ms
58,880 KB
testcase_36 AC 191 ms
59,108 KB
testcase_37 AC 188 ms
59,060 KB
testcase_38 AC 141 ms
55,824 KB
testcase_39 AC 159 ms
56,048 KB
testcase_40 AC 164 ms
58,220 KB
testcase_41 AC 142 ms
55,952 KB
testcase_42 AC 107 ms
56,048 KB
testcase_43 AC 107 ms
55,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class No1 {
    static final int INF = Integer.MAX_VALUE;
    public static void main (String[] args) {
        Scanner scan = new Scanner(System.in);

        int N = scan.nextInt();
        int C = scan.nextInt();
        int V = scan.nextInt();

        int[] S = new int[V];
        int[] T = new int[V];
        int[] Y = new int[V];
        int[] M = new int[V];
        for (int i = 0; i < V; i++) S[i] = scan.nextInt();
        for (int i = 0; i < V; i++) T[i] = scan.nextInt();
        for (int i = 0; i < V; i++) Y[i] = scan.nextInt();
        for (int i = 0; i < V; i++) M[i] = scan.nextInt();

        //わかりやすいように、n番目の街までに残金cで行くときの最小時間をmin_t[n][c+1]とする
        int[][] min_t = new int[N + 1][C + 1];
        for (int i = 1; i < N + 1; i++) {
            for (int j = 0; j < C + 1; j++) {
                min_t[i][j] = INF;
            }
        }
        //1番目の街は残金はMAXかつかかる時間は0
        min_t[1][C] = 0;

        //N番目の街はゴールなので探索しなくてもいい
        for (int i = 1; i < N; i++) {
            for (int j = 0; j < C + 1; j++) {
                //min_tがINFならその街に道はつながっていないので飛ばして良い
                if (min_t[i][j] != INF) {
                    for (int k = 0; k < V; k++) {
                        //道のリストに自分から出る道があり、かつその道を使った場合所持金がマイナスにならなければ最小時間を更新する
                        if ((S[k] == i) && (j - Y[k] >= 0)) {
                            min_t[T[k]][j - Y[k]] = Math.min(min_t[T[k]][j - Y[k]], min_t[i][j] + M[k]);
                        }
                    }
                }
            }
        }

        //N番目の街に行ける経路で最小時間のものを選択
        int ans = INF;
        for (int i = 0; i < C + 1; i++) ans = Math.min(ans, min_t[N][i]);
        //INFのままなら道がないということなので-1を代入
        if (ans == INF) ans = -1;

        System.out.print(ans);
    }
}
0