結果

問題 No.3393 Move on Highway
コンテスト
ユーザー t98slider
提出日時 2025-11-28 22:12:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 320 ms / 3,000 ms
コード長 2,438 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 213,328 KB
実行使用メモリ 19,068 KB
最終ジャッジ日時 2025-11-28 22:12:36
合計ジャッジ時間 11,917 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T> struct csr {
    using itr = typename std::vector<T>::iterator;
    struct Node {
        itr st, en;
        itr begin() { return st; }
        itr end() { return en; }
        int size() { return en - st; }
        T operator[](int p){ return st[p]; }
    };
    const int N;
    std::vector<int> start;
    std::vector<T> E;
    std::vector<std::pair<int,T>> edge;
    csr(int n) : N(n), start(n + 1) {}
    void add_edge(int u, T v){
        assert(0 <= u && u < N);
        start[u + 1]++;
        edge.emplace_back(u, v);
    }
    void build(){
        E.resize(edge.size());
        for(int i = 0; i < N; i++) start[i + 1] += start[i];
        auto cnt = start;
        for(auto [u, v] : edge) E[cnt[u]++] = v;
    }
    Node operator[](int p) {
        return Node{E.begin() + start[p], E.begin() + start[p + 1]};
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, c;
    cin >> n >> m >> c;
    csr<pair<int,int>> g(n);
    for(int i = 0; i < m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        g.add_edge(u, make_pair(v, w));
        g.add_edge(v, make_pair(u, w));
    }
    g.build();
    vector<ll> dp(n, 1ll << 60);
    vector<ll> dp2(2 * n, 1ll << 60);
    dp[0] = 0;
    dp2[n - 1] = 0;
    priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
    pq.emplace(0, 0);
    while(!pq.empty()){
        auto [d, v] = pq.top();
        pq.pop();
        if(d > dp[v]) continue;
        for(auto [u, w] : g[v]){
            if(d + w + c >= dp[u]) continue;
            dp[u] = d + w + c;
            pq.emplace(dp[u], u);
        }
    }
    pq.emplace(0, n - 1);
    while(!pq.empty()){
        auto [d, v] = pq.top();
        pq.pop();
        if(d > dp2[v]) continue;
        bool flg = false;
        if(v >= n){
            flg = true;
            v -= n;
        }
        for(auto [u, w] : g[v]){
            if(flg) u += n;
            if(d + w + c >= dp2[u]) continue;
            dp2[u] = d + w + c;
            pq.emplace(dp2[u], u);
        }
        if(flg) continue;
        for(auto [u, w] : g[v]){
            if(d + c >= dp2[u + n]) continue;
            dp2[u + n] = d + c;
            pq.emplace(dp2[u + n], u + n);
        }
    }
    for(int i = 1; i < n; i++){
        cout << min(dp[n - 1], dp[i] + dp2[i + n]) << '\n';
    }
}
0