結果

問題 No.1601 With Animals into Institute
ユーザー んんん
提出日時 2023-01-02 18:17:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 476 ms / 3,000 ms
コード長 1,343 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 212,116 KB
最終ジャッジ日時 2025-02-09 23:05:32
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using P = pair<long long, int>;
using Graph = vector<vector<P>>;

void dijkstra(const vector<vector<P>> &G, int s, vector<long long> &dist) {
    priority_queue<P, vector<P>, greater<P>> que;
    dist[s] = 0;
    que.emplace(dist[s], s);
    while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (dist[v] < p.first) continue;

        for (auto &[dis, nv] : G[v]) {
            if (dist[nv] > dist[v] + dis) {
                dist[nv] = dist[v] + dis;
                que.emplace(dist[nv], nv);
            }
        }
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    Graph G(N);
    vector<tuple<int, int, int, int>> abcx;
    for (int i = 0; i < M; i++) {
        int a, b, c, x;
        cin >> a >> b >> c >> x;
        a--; b--;
        G[a].emplace_back(c, b);
        G[b].emplace_back(c, a);

        abcx.emplace_back(a, b, c, x);
    }

    vector<long long> dist(N, 1LL << 60);
    dijkstra(G, N-1, dist);

    vector<P> v;
    for (auto &[a, b, c, x] : abcx) {
        if (x) {
            v.emplace_back(dist[a] + c, b);
            v.emplace_back(dist[b] + c, a);
        }
    }
    G.emplace_back(v);
    dist.assign(N+1, 1LL << 60);
    dijkstra(G, N, dist);

    for (int i = 0; i < N-1; i++) cout << dist[i] << endl;
}
0