結果

問題 No.160 最短経路のうち辞書順最小
ユーザー chocobochocobo
提出日時 2019-01-08 14:20:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,848 bytes
コンパイル時間 919 ms
コンパイル使用メモリ 104,836 KB
実行使用メモリ 5,272 KB
最終ジャッジ日時 2023-08-15 16:42:32
合計ジャッジ時間 2,432 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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});
            }
        }
    }
    
    string ans;
    ans += '0' + 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 += '0' + mn;
    }
    
    REP(i, ans.size()){
        cout << ans[i] << " \n"[i == ans.size() - 1];
    }
}
0