結果

問題 No.1 道のショートカット
ユーザー commycommy
提出日時 2018-08-10 23:25:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,933 bytes
コンパイル時間 1,006 ms
コンパイル使用メモリ 91,260 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 22:15:23
合計ジャッジ時間 2,383 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

struct Edge {
    lli t, y, m;
    Edge(lli _t, lli _y, lli _m)
        : t(_t), y(_y), m(_m) {}
    Edge() {}
    bool operator<(Edge const &e) const {
        return m > e.m;
    }
};

int main() {
    int N, C, V;
    cin >> N >> C >> V;
    vector<int> S(V), T(V), Y(V), M(V);
    REP(i, 0, V) {
        cin >> S[i];
    }
    REP(i, 0, V) {
        cin >> T[i];
    }
    REP(i, 0, V) {
        cin >> Y[i];
    }
    REP(i, 0, V) {
        cin >> M[i];
    }
    vector<vector<Edge>> G(N);
    REP(i, 0, V) {
        G[S[i] - 1].push_back(Edge(T[i] - 1, Y[i], M[i]));
    }
    const lli inf = 1LL << 60;
    priority_queue<Edge> pq;
    pq.emplace(0, 0, 0);
    auto cost = make_v(N, C + 1, inf);
    auto used = make_v(N, C + 1, false);
    cost[0][0] = 0;
    lli ans = inf;
    while (pq.size()) {
        auto elem = pq.top();
        pq.pop();
        if (used[elem.t][elem.y]) continue;
        used[elem.t][elem.y] = true;
        if (elem.t == N - 1) {
            ans = min(ans, elem.m);
            continue;
        }
        for (auto &e : G[elem.t]) {
            lli ny = elem.y + e.y;
            lli nm = elem.m + e.m;
            if (ny > C) {
                continue;
            }
            if (cost[e.t][ny] > nm) {
                cost[e.t][ny] = nm;
                pq.emplace(e.t, ny, nm);
            }
        }
    }
    if (ans == inf) {
        ans = -1;
    }
    cout << ans << endl;
    return 0;
}
0