結果

問題 No.160 最短経路のうち辞書順最小
ユーザー noisy_noiminnoisy_noimin
提出日時 2020-10-24 17:06:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 2,176 bytes
コンパイル時間 1,321 ms
コンパイル使用メモリ 127,464 KB
実行使用メモリ 10,436 KB
最終ジャッジ日時 2023-09-28 20:30:35
合計ジャッジ時間 2,808 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,332 KB
testcase_01 AC 6 ms
9,288 KB
testcase_02 AC 6 ms
9,296 KB
testcase_03 AC 6 ms
9,156 KB
testcase_04 AC 7 ms
9,604 KB
testcase_05 AC 8 ms
9,796 KB
testcase_06 AC 9 ms
9,884 KB
testcase_07 AC 6 ms
9,400 KB
testcase_08 AC 7 ms
9,284 KB
testcase_09 AC 7 ms
9,400 KB
testcase_10 AC 7 ms
9,200 KB
testcase_11 AC 7 ms
9,328 KB
testcase_12 AC 7 ms
9,328 KB
testcase_13 AC 6 ms
9,272 KB
testcase_14 AC 6 ms
9,212 KB
testcase_15 AC 7 ms
9,296 KB
testcase_16 AC 6 ms
9,156 KB
testcase_17 AC 7 ms
9,152 KB
testcase_18 AC 7 ms
9,268 KB
testcase_19 AC 7 ms
9,392 KB
testcase_20 AC 6 ms
9,220 KB
testcase_21 AC 7 ms
9,392 KB
testcase_22 AC 7 ms
9,600 KB
testcase_23 AC 7 ms
9,280 KB
testcase_24 AC 6 ms
9,248 KB
testcase_25 AC 7 ms
9,152 KB
testcase_26 AC 6 ms
9,324 KB
testcase_27 AC 6 ms
9,384 KB
testcase_28 AC 12 ms
10,436 KB
testcase_29 AC 6 ms
9,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};
constexpr int MAX_N = 112345;

vector<ll> d(MAX_N, LINF);
vector<int> path, prevs[MAX_N];
int n, m;
priority_queue<Pll, vector<Pll>, greater<Pll> > que;
vector<Pll> edges[MAX_N];

void dijkstra(int start, int goal){
    d[start] = 0;
    que.push(Pll(0, start));

    while(!que.empty()) {
        Pll v = que.top(); que.pop();
        if(d[v.second] < v.first) continue;
        for(Pll e: edges[v.second]) {
            if(d[e.first] > d[v.second] + e.second) {
                d[e.first] = d[v.second] + e.second;
                prevs[e.first].clear();
                prevs[e.first].push_back(v.second);
                que.push(Pll(d[e.first], e.first));
            } else if(d[e.first] == d[v.second] + e.second) {
                prevs[e.first].push_back(v.second);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int start, goal;
    cin >> n >> m >> start >> goal;
    int a[m], b[m], c[m];
    for(int i=0;i<m;++i) {
        cin >> a[i] >> b[i] >> c[i];
        edges[b[i]].emplace_back(a[i], c[i]);
        edges[a[i]].emplace_back(b[i], c[i]);
    }

    dijkstra(goal, start);
    if(d[start] == LINF) {
        cout << -1 << endl;
        return 0;
    }

    int v = start;
    while(v != goal) {
        path.push_back(v);
        v = *min_element(prevs[v].begin(), prevs[v].end());
    }
    path.push_back(goal);

    for(int i=0;i<path.size();++i) {
        if(i) cout << " ";
        cout << path[i];
    }
    cout << endl;
    return 0;
}
0