結果

問題 No.160 最短経路のうち辞書順最小
ユーザー noisy_noiminnoisy_noimin
提出日時 2020-10-24 17:04:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,127 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 128,768 KB
実行使用メモリ 9,992 KB
最終ジャッジ日時 2023-09-28 20:30:29
合計ジャッジ時間 3,089 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,164 KB
testcase_01 WA -
testcase_02 AC 6 ms
9,244 KB
testcase_03 WA -
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 AC 6 ms
9,176 KB
testcase_28 WA -
testcase_29 AC 6 ms
9,284 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], 1);
    }

    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