結果

問題 No.1037 exhausted
ユーザー noriocnorioc
提出日時 2020-04-25 05:25:24
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,453 bytes
コンパイル時間 1,691 ms
コンパイル使用メモリ 166,324 KB
実行使用メモリ 37,544 KB
最終ジャッジ日時 2023-09-04 07:30:21
合計ジャッジ時間 2,931 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 37 ms
36,256 KB
testcase_13 AC 37 ms
36,716 KB
testcase_14 AC 39 ms
36,500 KB
testcase_15 AC 37 ms
36,992 KB
testcase_16 AC 40 ms
36,560 KB
testcase_17 AC 39 ms
36,712 KB
testcase_18 AC 41 ms
36,236 KB
testcase_19 AC 40 ms
37,544 KB
testcase_20 AC 23 ms
36,932 KB
testcase_21 AC 19 ms
36,672 KB
testcase_22 AC 25 ms
36,944 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 k = min(v, j + ss[i].gas);
            if (k >= d)
                dp[i + 1][k - d] = min(dp[i + 1][k - 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