結果

問題 No.3111 Toll Optimization
ユーザー ont
提出日時 2025-06-30 21:30:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 224 ms / 5,000 ms
コード長 1,789 bytes
コンパイル時間 5,529 ms
コンパイル使用メモリ 335,612 KB
実行使用メモリ 19,780 KB
最終ジャッジ日時 2025-06-30 21:30:30
合計ジャッジ時間 13,643 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define NP next_permutation
const int INF32 = 2e9;
const ll INF64 = 2e18;
const ll mod = 998244353;
// const ll mod = 1e9 + 7;
template<typename T> bool chmin(T &a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T &a, T b){if(a < b){a = b; return true;} return false;}
 
void cincout() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
}
 
int main() {
    cincout();
 
    int N, M, K;
    cin >> N >> M >> K;
    
    vector<int> C(M);
    for (int i = 0; i < M; i++) cin >> C[i];

    using P = pair<ll, ll>;
    vector<vector<P>> graph(N);
    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]);
    }

    using A = array<ll, 3>;
    priority_queue<A, vector<A>, greater<A>> pq;
    pq.push({0, 0, 0});
    vector<vector<ll>> dist(N, vector<ll>(K + 1, INF64));
    dist[0][0] = 0;

    while (!pq.empty()) {
        auto [d, v, k] = pq.top(); 
        pq.pop();
        if (dist[v][k] < d) continue;

        for (auto [next_v, w] : graph[v]) {
            if (chmin(dist[next_v][k], d + w)) pq.push({dist[next_v][k], next_v, k});
            if (k < K && chmin(dist[next_v][k + 1], d)) pq.push({dist[next_v][k + 1], next_v, k + 1}); 
        } 
    }

    ll ans = *min_element(all(dist[N - 1]));
    cout << (ans != INF64 ? ans : -1) << endl;
}
0