結果

問題 No.1 道のショートカット
ユーザー yuki2006yuki2006
提出日時 2014-10-11 03:17:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 244 ms / 5,000 ms
コード長 2,797 bytes
コンパイル時間 5,800 ms
コンパイル使用メモリ 82,632 KB
実行使用メモリ 61,520 KB
最終ジャッジ日時 2023-09-27 21:35:42
合計ジャッジ時間 15,076 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
56,052 KB
testcase_01 AC 115 ms
55,956 KB
testcase_02 AC 111 ms
56,356 KB
testcase_03 AC 120 ms
55,620 KB
testcase_04 AC 117 ms
55,732 KB
testcase_05 AC 113 ms
56,092 KB
testcase_06 AC 121 ms
55,660 KB
testcase_07 AC 125 ms
55,856 KB
testcase_08 AC 220 ms
59,920 KB
testcase_09 AC 204 ms
60,728 KB
testcase_10 AC 215 ms
59,732 KB
testcase_11 AC 220 ms
61,324 KB
testcase_12 AC 244 ms
61,252 KB
testcase_13 AC 233 ms
60,812 KB
testcase_14 AC 161 ms
55,948 KB
testcase_15 AC 113 ms
55,936 KB
testcase_16 AC 181 ms
58,884 KB
testcase_17 AC 121 ms
56,220 KB
testcase_18 AC 158 ms
56,008 KB
testcase_19 AC 159 ms
56,148 KB
testcase_20 AC 182 ms
58,084 KB
testcase_21 AC 167 ms
56,468 KB
testcase_22 AC 140 ms
56,232 KB
testcase_23 AC 232 ms
60,524 KB
testcase_24 AC 232 ms
60,956 KB
testcase_25 AC 189 ms
58,220 KB
testcase_26 AC 174 ms
56,964 KB
testcase_27 AC 224 ms
61,520 KB
testcase_28 AC 125 ms
55,720 KB
testcase_29 AC 215 ms
59,340 KB
testcase_30 AC 181 ms
59,236 KB
testcase_31 AC 194 ms
58,764 KB
testcase_32 AC 211 ms
60,760 KB
testcase_33 AC 198 ms
60,016 KB
testcase_34 AC 233 ms
60,884 KB
testcase_35 AC 196 ms
58,888 KB
testcase_36 AC 210 ms
59,564 KB
testcase_37 AC 200 ms
59,048 KB
testcase_38 AC 139 ms
56,172 KB
testcase_39 AC 171 ms
55,880 KB
testcase_40 AC 186 ms
59,092 KB
testcase_41 AC 155 ms
56,156 KB
testcase_42 AC 114 ms
55,980 KB
testcase_43 AC 116 ms
56,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Yuki001 {

    public static class Tuple implements Comparable<Tuple> {
        int cost;
        int time;

        Tuple(int cost, int time) {
            this.cost = cost;
            this.time = time;
        }

        @Override
        public int compareTo(Tuple o) {
            return -Integer.compare(cost, o.cost);
        }
    }

    public static void main(String[] args) {


        ArrayList<Integer> si = new ArrayList<Integer>();
        ArrayList<Integer> ti = new ArrayList<Integer>();

        Scanner scanner = new Scanner(System.in);

        int N = scanner.nextInt();
        int C = scanner.nextInt();

        HashMap<Integer, ArrayList<Tuple>> map = new HashMap<>();


        int V = scanner.nextInt();
        int[] cost = new int[V + 1];


        for (int i = 0; i < V; i++) {
            si.add(scanner.nextInt());
        }
        for (int i = 0; i < V; i++) {
            ti.add(scanner.nextInt());
        }
        for (int i = 0; i < V; i++) {
            cost[i] = scanner.nextInt();
        }
        for (int i = 0; i < V; i++) {

            final int key = ti.get(i) * (N + 1) + si.get(i);
            ArrayList<Tuple> tuples = map.get(key);
            if (tuples == null) {
                tuples = new ArrayList<>();
                map.put(key, tuples);
            }
            tuples.add(new Tuple(cost[i], scanner.nextInt()));
        }


        int MAX = Integer.MAX_VALUE / 2;

        int[][] dp = new int[N + 1][C + 1];

        for (int i = 0; i < dp.length; i++) {
            Arrays.fill(dp[i], MAX);
        }
        dp[1][C] = 0;
        for (int i = 2; i < N + 1; i++) {
            for (int k = 1; k < i; k++) {
                final ArrayList<Tuple> tuples = map.get(i * (N + 1) + k);
                if (tuples == null) {
                    continue;
                }
                for (int t = 0; t <= C; t++) {
                    if (dp[k][t] >= MAX) {
                        continue;
                    }
                    for (Tuple tuple : tuples) {

                        int c = t - tuple.cost;
                        if (c < 0) {
                            continue;
                        }
                        dp[i][c] = Math.min(dp[i][c], dp[k][t] + tuple.time);
                    }
                }
            }
        }

        int min = Integer.MAX_VALUE;
        for (int i = C; i >= 0; i--) {
            if (dp[N][i] != MAX) {
                min = Math.min(min, dp[N][i]);
            }
        }

        if (min == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(min);

        }

    }

}
0