結果

問題 No.160 最短経路のうち辞書順最小
ユーザー FlkanjinFlkanjin
提出日時 2022-01-05 16:29:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 3,388 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 158,256 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 20:16:22
合計ジャッジ時間 3,004 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 10 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 4 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 3 ms
5,376 KB
testcase_25 AC 4 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 #

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10L;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){a = b; return true;}
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){a = b; return true;}
    return false;
}
template<class T, class Container> inline bool exist(Container &s, const T &e){
    return (s.find(e) != std::end(s));
}
template<class T> inline bool inside(T x, T lx, T rx){
    return (std::clamp(x, lx, rx) == x);
}
template<class T> inline bool inside(T x, T y, T lx, T rx, T ly, T ry){
    return inside(x, lx, rx) && inside(y, ly, ry);
}








struct edge{
    int to, cost;
    edge(int To, int Cost): to(To), cost(Cost){}
};

void dijkstra(int s, std::vector<std::vector<edge>> &G, std::vector<long long> &d, std::vector<int> &prv){
    //pair<int, int> first: 距離 second: 頂点
    std::priority_queue<std::pair<long long, int>,
                        std::vector<std::pair<long long, int>>,
                        std::greater<std::pair<long long, int>>> que;
    int V{int(G.size())};
    d.resize(V, LINF+3);
    prv.resize(V, NIL);
    d[s] = 0;
    que.emplace(0, s);

    while(!que.empty()){
        std::pair<long long, int> p{que.top()}; que.pop();
        int v{p.second};
        if(d[v] < p.first) continue;
        for(edge &e: G[v]){
            if(d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                prv[e.to] = v;
                que.emplace(d[e.to], e.to);
            }
        }
    }
}

int main(){
    int N, M, S, G; std::cin >> N >> M >> S >> G;
    std::vector<std::vector<edge>> graph(N);
    {
        int a, b, c;
        for(int i{0}; i < M; ++i){
            std::cin >> a >> b >> c;
            graph[a].emplace_back(b, c);
            graph[b].emplace_back(a, c);
        }
    }
    /*for(int i{0}; i < N; ++i){
        std::sort(std::begin(graph[i]), std::end(graph[i]), [](edge &a, edge &b){return a.to < b.to;});
    }*/
    std::vector<int> prv;
    std::vector<long long> d;
    dijkstra(G, graph, d, prv);
    std::vector<int> ans;
    ans.reserve(N);
    ans.push_back(S);
    int cur{S};
    while(cur != G){
        int dist{int(d[cur])}, v{N};
        for(auto [to, cost]: graph[cur]){
            if(d[to] + cost == dist) chmin(v, to);
        }
        cur = v;
        ans.push_back(v);
    }
    for(auto itr{std::begin(ans)}; itr != std::end(ans); ++itr){
        if(itr != std::begin(ans)) std::cout << " ";
        std::cout << *itr;
    }
    std::cout << std::endl;
    return 0;
}
0