結果

問題 No.1037 exhausted
ユーザー htensaihtensai
提出日時 2020-04-30 08:46:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,890 ms / 2,000 ms
コード長 2,209 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 76,160 KB
実行使用メモリ 273,652 KB
最終ジャッジ日時 2023-08-21 04:04:04
合計ジャッジ時間 19,998 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,540 KB
testcase_01 AC 123 ms
56,220 KB
testcase_02 AC 122 ms
55,800 KB
testcase_03 AC 124 ms
57,540 KB
testcase_04 AC 123 ms
55,700 KB
testcase_05 AC 124 ms
56,172 KB
testcase_06 AC 128 ms
55,820 KB
testcase_07 AC 126 ms
56,304 KB
testcase_08 AC 125 ms
55,796 KB
testcase_09 AC 127 ms
55,952 KB
testcase_10 AC 127 ms
55,828 KB
testcase_11 AC 129 ms
57,532 KB
testcase_12 AC 1,635 ms
222,704 KB
testcase_13 AC 1,636 ms
224,872 KB
testcase_14 AC 1,777 ms
273,652 KB
testcase_15 AC 1,797 ms
225,268 KB
testcase_16 AC 1,776 ms
265,312 KB
testcase_17 AC 1,780 ms
272,184 KB
testcase_18 AC 1,890 ms
271,904 KB
testcase_19 AC 1,786 ms
271,620 KB
testcase_20 AC 232 ms
59,472 KB
testcase_21 AC 232 ms
59,200 KB
testcase_22 AC 230 ms
59,480 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 v = sc.nextInt();
        int l = sc.nextInt();
        GS[] stations = new GS[n + 2];
        for (int i = 1; i <= n; i++) {
            stations[i] = new GS(sc.nextInt(), sc.nextInt(), sc.nextInt());
        }
        stations[0] = new GS(0, 0, 0);
        stations[n + 1] = new GS(l, 0, 0);
        ArrayList<HashMap<Integer, Long>> dp = new ArrayList<>();
        for (int i = 0; i <= n + 1; i++) {
            dp.add(new HashMap<>());
        }
        dp.get(0).put(v, 0L);
        for (int i = 1; i <= n + 1; i++) {
            int length = stations[i].point - stations[i - 1].point;
            HashMap<Integer, Long> map = dp.get(i);
            for (Map.Entry<Integer, Long> entry : dp.get(i - 1).entrySet()) {
                int gas = entry.getKey() - length;
                if (gas < 0) {
                    continue;
                }
                if (map.containsKey(gas)) {
                    if (map.get(gas) > entry.getValue()) {
                        map.put(gas, entry.getValue());
                    }
                } else {
                    map.put(gas, entry.getValue());
                }
                long cost = entry.getValue() + stations[i].cost;
                gas = Math.min(v, gas + stations[i].gas);
                if (map.containsKey(gas)) {
                    if (map.get(gas) > cost) {
                        map.put(gas, cost);
                    }
                } else {
                    map.put(gas, cost);
                }
            }
        }
        if (dp.get(n + 1).size() == 0) {
            System.out.println(-1);
            return;
        }
        long min = Long.MAX_VALUE;
        for (long x : dp.get(n + 1).values()) {
            min = Math.min(x, min);
        }
        System.out.println(min);
    }
    
    static class GS {
        int point;
        int gas;
        int cost;
        
        public GS(int point, int gas, int cost) {
            this.point = point;
            this.gas = gas;
            this.cost = cost;
        }
    }
}
0