結果

問題 No.3111 Toll Optimization
ユーザー Rira
提出日時 2025-04-20 14:32:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 263 ms / 5,000 ms
コード長 1,564 bytes
コンパイル時間 2,697 ms
コンパイル使用メモリ 211,264 KB
実行使用メモリ 19,784 KB
最終ジャッジ日時 2025-04-20 14:32:40
合計ジャッジ時間 11,578 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = tuple<ll, int, int>; // {cost, node, used_coupon}

const ll INF = 1LL << 60;

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

    int N, M, K;
    cin >> N >> M >> K;

    vector<ll> C(M);
    for (int i = 0; i < M; ++i) cin >> C[i];

    vector<vector<pair<int, ll>>> graph(N); // graph[u] = {v, cost}
    for (int i = 0; i < M; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        graph[u].emplace_back(v, C[i]);
        graph[v].emplace_back(u, C[i]);
    }

    // dist[node][used_coupon] = 最小コスト
    vector<vector<ll>> dist(N, vector<ll>(K + 1, INF));
    dist[0][0] = 0;

    priority_queue<P, vector<P>, greater<P>> pq;
    pq.emplace(0, 0, 0); // {cost, node, used_coupon}

    while (!pq.empty()) {
        auto [cost, node, used] = pq.top(); pq.pop();
        if (dist[node][used] < cost) continue;

        for (auto [nei, c] : graph[node]) {
            // クーポンを使わない場合
            if (dist[nei][used] > cost + c) {
                dist[nei][used] = cost + c;
                pq.emplace(cost + c, nei, used);
            }
            // クーポンを使う場合(残っていれば)
            if (used < K && dist[nei][used + 1] > cost) {
                dist[nei][used + 1] = cost;
                pq.emplace(cost, nei, used + 1);
            }
        }
    }

    ll ans = *min_element(dist[N - 1].begin(), dist[N - 1].end());
    cout << (ans == INF ? -1 : ans) << '\n';

    return 0;
}
0