結果

問題 No.160 最短経路のうち辞書順最小
ユーザー maine_honzuki
提出日時 2020-06-01 15:00:18
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,149 bytes
コンパイル時間 1,885 ms
コンパイル使用メモリ 173,384 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-21 19:12:32
合計ジャッジ時間 2,819 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    int N, M, S, G;
    vector<pair<int, int>> V[210];
    cin >> N >> M >> S >> G;
    for (int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        V[a].push_back({b, c});
        V[b].push_back({a, c});
    }

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
    que.push({0, G});

    int dist[210] = {}, nxt[210] = {};
    fill(dist, dist + N, 1e7);
    fill(nxt, nxt + N, -1);
    dist[G] = 0;

    while (!que.empty()) {
        auto p = que.top();
        que.pop();

        int d = p.first, v = p.second;
        if (dist[v] < d)
            continue;

        for (auto& e : V[v]) {
            int u = e.first, cost = e.second;
            if (nxt[u] == -1 || dist[u] > d + cost) {
                dist[u] = d + cost;
                nxt[u] = v;
                que.push({dist[u], u});
            } else if (dist[u] == d + cost && nxt[u] > v)
                nxt[u] = v;
        }
    }

    int now = S;
    while (now != G) {
        cout << now << " ";
        now = nxt[now];
    }
    cout << G << endl;
}
0