結果

問題 No.1 道のショートカット
コンテスト
ユーザー SnowBeenDiding
提出日時 2026-01-09 09:29:35
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 2,418 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 10,795 ms
コンパイル使用メモリ 429,176 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2026-01-09 09:29:48
合計ジャッジ時間 12,930 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#include <atcoder/all>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace atcoder;
using namespace std;

typedef long long ll;

template <typename T>
struct Graph {
    struct edge {
        int to;
        T cost;
    };

    int n;
    vector<vector<edge>> gr;
    vector<T> dist;
    vector<int> prev;
    T inf, zero;

    Graph(int n, T inf, T zero) : n(n), inf(inf), zero(zero) { init(n); }

    void init(int n_) {
        gr.assign(n, {});
        dist.assign(n, inf);
        prev.assign(n, -1);
    }

    void add_edge(int from, int to, T cost) { gr[from].push_back({to, cost}); }

    void dijkstra(int s) {
        fill(dist.begin(), dist.end(), inf);
        fill(prev.begin(), prev.end(), -1);

        priority_queue<pair<T, int>, vector<pair<T, int>>,
                       greater<pair<T, int>>>
            pq;
        dist[s] = zero;
        pq.push({zero, s});
        while (!pq.empty()) {
            auto [d, v] = pq.top();
            pq.pop();

            if (dist[v] < d) continue;

            for (const auto& e : gr[v]) {
                T nd = d + e.cost;
                if (dist[e.to] > nd) {
                    dist[e.to] = nd;
                    prev[e.to] = v;
                    pq.push({nd, e.to});
                }
            }
        }
    }

    vector<int> get_path(int t) const {
        if (dist[t] == inf) return {-1};
        vector<int> path;
        for (; t != -1; t = prev[t]) {
            path.push_back(t);
        }
        reverse(path.begin(), path.end());
        return path;
    }
};

void solve() {
    int n, c, m;
    cin >> n >> c >> m;
    vector<int> u(m), v(m), x(m), y(m);
    rep(i, 0, m) cin >> u[i], u[i]--;
    rep(i, 0, m) cin >> v[i], v[i]--;
    rep(i, 0, m) cin >> x[i];
    rep(i, 0, m) cin >> y[i];
    int inf = 1e9 + 10;
    int block = c + 10;
    Graph<int> gr(n * block, inf, 0);
    rep(i, 0, m) {
        for (int j = 0; j + x[i] <= c; j++) {
            gr.add_edge(u[i] * block + j, v[i] * block + j + x[i], y[i]);
        }
    }
    gr.dijkstra(0);
    int ans = inf;
    rep(i, 0, block) { ans = min(ans, gr.dist[(n - 1) * block + i]); }
    if (ans == inf) ans = -1;
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
0