結果

問題 No.160 最短経路のうち辞書順最小
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-25 19:02:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,385 bytes
コンパイル時間 1,269 ms
コンパイル使用メモリ 122,540 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 23:46:41
合計ジャッジ時間 2,199 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 11 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 15 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>

using namespace std;

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;
 
int main(){
 
    long long N, M, A, B, C, S, G;
    long long from, alt, d;
    cin >> N >> M >> S >> G;
    pq<pair<long long, long long>> que;
    vector<vector<pair<long long, long long>>> E(N);
    vector<long long> dist(N, 1e18), ans;
    vector<bool> visit(N);
 
    for (int i=0; i < M; i++){
        cin >> A >> B >> C;
        E[A].push_back({B, C});
        E[B].push_back({A, C});
    }
    for (auto &x : E) sort(x.begin(), x.end());
 
    dist[G] = 0;
    que.push({0, G});
 
    while(!que.empty()){
        tie(d, from) = que.top();
        que.pop();
        if (visit[from]) continue;
        visit[from] = 1;
        for (auto [to, C] : E[from]){
            alt = d + C;
            if (alt < dist[to]){
                dist[to] = alt;
                que.push({dist[to], to});
            }
        }
    }

    ans.push_back(S);
    long long now = S;

    while(now != G){
        for (auto [x, y] : E[now]){
            if (dist[x] == dist[now] - y){
                now = x;
                ans.push_back(now);
            }
        }
    }

    for (auto x : ans) cout << x << " ";
    cout << endl;
 
    return 0;
}
0