結果

問題 No.3111 Toll Optimization
ユーザー しとりん
提出日時 2025-04-20 13:44:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 188 ms / 5,000 ms
コード長 1,345 bytes
コンパイル時間 2,380 ms
コンパイル使用メモリ 213,408 KB
実行使用メモリ 21,304 KB
最終ジャッジ日時 2025-04-20 13:45:06
合計ジャッジ時間 8,897 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
static 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<tuple<int,ll,int>>> G(N+1);
    for(int i = 0; i < M; i++){
        int u,v;
        cin >> u >> v;
        ll c = C[i];
        G[u].emplace_back(v, c, i);
        G[v].emplace_back(u, c, i);
    }

    vector<vector<ll>> dist(N+1, vector<ll>(K+1, INF));
    using T = tuple<ll,int,int>;
    priority_queue<T, vector<T>, greater<T>> pq;
    dist[1][0] = 0;
    pq.emplace(0, 1, 0);

    while(!pq.empty()){
        auto [d, u, k] = pq.top(); pq.pop();
        if(d > dist[u][k]) continue;
        for(auto &[v, cost, idx] : G[u]){
            ll nd = d + cost;
            if(nd < dist[v][k]){
                dist[v][k] = nd;
                pq.emplace(nd, v, k);
            }
            if(k < K){
                if(d < dist[v][k+1]){
                    dist[v][k+1] = d;
                    pq.emplace(d, v, k+1);
                }
            }
        }
    }

    ll ans = INF;
    for(int k = 0; k <= K; k++){
        ans = min(ans, dist[N][k]);
    }
    if(ans == INF) ans = -1;
    cout << ans << "\n";
    return 0;
}
0