結果

問題 No.1037 exhausted
ユーザー noriocnorioc
提出日時 2020-04-25 04:21:59
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,484 bytes
コンパイル時間 2,021 ms
コンパイル使用メモリ 166,320 KB
実行使用メモリ 36,984 KB
最終ジャッジ日時 2023-09-04 07:30:17
合計ジャッジ時間 3,639 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 38 ms
36,716 KB
testcase_13 AC 38 ms
35,996 KB
testcase_14 AC 39 ms
36,720 KB
testcase_15 AC 38 ms
35,968 KB
testcase_16 AC 41 ms
36,984 KB
testcase_17 AC 41 ms
36,188 KB
testcase_18 AC 42 ms
36,740 KB
testcase_19 AC 41 ms
35,968 KB
testcase_20 AC 28 ms
36,252 KB
testcase_21 AC 21 ms
36,720 KB
testcase_22 AC 25 ms
36,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

alias GasStation = Tuple!(int, "x", int, "gas", int, "cost");

long calc(int v, int l, GasStation[] ss) {
    int n = cast(int)ss.length;

    ss ~= GasStation(l, 0, 0); // sentinel
    // dp[i 番目のガソリンスタンド][ガソリンの量]
    auto dp = new long[][](n + 1, v + 1);
    foreach (i; 0..dp.length) dp[i][] = long.max;

    if (ss[0].x > v) return -1;
    dp[0][v - ss[0].x] = 0;

    for (int i = 0; i < n; i++) {
        int d = ss[i + 1].x - ss[i].x;
        for (int j = 0; j <= v; j++) {
            if (dp[i][j] == long.max) continue;

            if (j >= d) {
                // 補給なしで進む
                dp[i + 1][j - d] = min(dp[i + 1][j - d], dp[i][j]);
            }

            // 補給して進む
            auto v2 = min(v, j + ss[i].gas);
            if (v2 >= d) {
                dp[i + 1][v2 - d] = min(dp[i + 1][v2 - d], dp[i][j] + ss[i].cost);
            }
        }
    }

    long ans = dp[n].minElement;
    return ans == long.max ? -1 : ans;
}

void main() {
    int n, v, l; scan(n, v, l);
    GasStation[] ss;
    foreach (_; 0..n) {
        int x, gas, cost; scan(x, gas, cost);
        ss ~= GasStation(x, gas, cost);
    }
    writeln(calc(v, l, ss));
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
0