結果

問題 No.160 最短経路のうち辞書順最小
ユーザー chocobochocobo
提出日時 2019-01-08 14:22:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,856 bytes
コンパイル時間 956 ms
コンパイル使用メモリ 105,724 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 16:42:35
合計ジャッジ時間 2,099 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)








bool comp(vector<ll> &a, vector<ll> &b){
    REP(i, min(a.size(), b.size())){
        if(a[i] != b[i]) return a[i] > b[i];
    }
    return a.size() > b.size();
}

using P = pair<ll, ll>;

int main() {
    ll n, m, S, G;
    cin >> n >> m >> S >> G;
    vector<vector<P>> g(n);
    REP(i, m){
        ll a, b, c;
        cin >> a >> b >> c;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    
    vector<ll> dist(n, INF);
    dist[G] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, G});
    
    while(!que.empty()){
        auto tmp = que.top(); que.pop();
        ll d = tmp.first, now = tmp.second;
        if(dist[now] < d) continue;
        
        for(auto &x : g[now]){
            ll v = x.first, c = x.second;
            ll cost = dist[now] + c;
            
            if(dist[v] > cost){
                dist[v] = cost;
                que.push({cost, v});
            }
        }
    }
    
    vector<ll> ans;
    ans.push_back(S);
    for(ll now = S; now != G;){
        ll mn = INF;
        for(auto &x : g[now]){
            ll v = x.first, c = x.second;
            if(dist[now] == dist[v] + c){
                mn = min(mn, v);
            }
        }
        now = mn;
        ans.push_back(mn);
    }
    
    REP(i, ans.size()){
        cout << ans[i] << " \n"[i == ans.size() - 1];
    }
}
0