結果

問題 No.1 道のショートカット
ユーザー H3PO4H3PO4
提出日時 2023-02-10 16:59:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,521 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 82,620 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 19:22:44
合計ジャッジ時間 2,729 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,384 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,384 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

int main() {
    int N, C, V;
    std::cin >> N;
    std::cin >> C;
    std::cin >> V;
    std::vector<int> S(V), T(V), Y(V), M(V);
    for (int i = 0; i < V; i++) {
        std::cin >> S[i];
        S[i] -= 1;
    }
    for (int i = 0; i < V; i++) {
        std::cin >> T[i];
        T[i] -= 1;
    }
    for (int i = 0; i < V; i++) { std::cin >> Y[i]; }
    for (int i = 0; i < V; i++) { std::cin >> M[i]; }

    const int INF = 100000;

    std::vector<std::vector<int>> dp(N, std::vector<int>(C + 1, INF));
    dp.at(0).at(0) = 0;

    struct Edge {
        int t;
        int y;
        int m;
    };
    std::vector<std::vector<Edge>> G(N, std::vector<Edge>());
    for (int i = 0; i < V; i++) {
        Edge edge{T.at(i), Y.at(i), M.at(i)};
        G.at(S.at(i)).push_back(edge);
    }

    for (int s = 0; s < N; s++) {
        for (int c = 0; c < C; c++) {
            if (dp.at(s).at(c) == INF) {
                continue;
            }
            for (const auto &edge: G.at(s)) {
                if (c + edge.y > C) {
                    continue;
                }
                if (dp.at(edge.t).at(c + edge.y) > dp.at(s).at(c) + edge.m) {
                    dp.at(edge.t).at(c + edge.y) = dp.at(s).at(c) + edge.m;
                }
            }
        }
    }
    int ans = INF;
    for (const auto &x: dp.at(N - 1)) {
        if (ans > x) {
            ans = x;
        }
    }
    if (ans == INF) {
        ans = -1;
    }
    std::cout << ans << std::endl;
}
0