結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ningenMeningenMe
提出日時 2020-10-24 18:43:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,392 bytes
コンパイル時間 2,565 ms
コンパイル使用メモリ 212,708 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 20:37:01
合計ジャッジ時間 3,993 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int N,M,S,G;
    cin >> N >> M >> S >> G;
    vector<vector<pair<int,int>>> edge(N);
    for(int i=0;i<M;++i){
        int a,b,c; cin >> a >> b >> c;
        edge[a].push_back({b,c});
        edge[b].push_back({a,c});
    }
    for(int i=0;i<N;++i) sort(edge[i].begin(),edge[i].end());
    int inf = 12345678;
    vector<int> dp(N,inf);
    priority_queue_reverse<pair<int,int>> pq;
    dp[G]=0;
    pq.push({0,G});
    while(pq.size()) {
        auto p = pq.top(); pq.pop();
        int from = p.second;
        if(p.first > dp[from]) continue;
        for(auto q:edge[from]) {
            int to = q.first;
            if(dp[to] > dp[from]+q.second) {
                dp[to] = dp[from]+q.second;
                pq.push({dp[to],to});
            }
        }
    }
    vector<int> ans = {S};
    int sum = 0;
    while(ans.back()!=G) {
        int from = ans.back();
        for(auto q:edge[from]) {
            int to = q.first;
            if(sum + q.second + dp[to] == dp[S]) {
                ans.push_back(to);
                sum += q.second;
                break;
            }
        }
    }
    for(int i=0;i<ans.size();++i) cout << ans[i] << " \n"[i==ans.size()-1];
    return 0;
}
0