結果

問題 No.160 最短経路のうち辞書順最小
ユーザー nmgm221nmgm221
提出日時 2017-04-19 01:31:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,581 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 92,892 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 13:30:15
合計ジャッジ時間 2,418 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <numeric>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define itr(i, x) for(auto i = (x).begin(); i != (x).end(); i++)
#define ritr(i, x) for(auto i = (x).rbegin(); i != (x).rend(); i++)
#define INF 1010101010
#define MOD 1000000007
#define LL long long

int main() {
    int n, m, s, g;
    cin >> n >> m >> s >> g;
    vector<vector<pair<int, int>>> gr(n);
    vector<int> mdist(n, INF);
    vector<bool> used(n, false);
    vector<int> pre(n);
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        gr[a].push_back(make_pair(b, c));
        gr[b].push_back(make_pair(a, c));
    }
    mdist[g] = 0;
    pre[g] = g;

    while(!used[s]) {
        int next = -1;
        rep(i, n)
            next = !used[i] && (next == -1 || mdist[i] < mdist[next]) ? i : next;
        used[next] = true;
        for (const auto e: gr[next]) {
            if (mdist[next] + e.second < mdist[e.first]) {
                pre[e.first] = next;
                mdist[e.first] = mdist[next] + e.second;
            }
            if (mdist[next] + e.second == mdist[e.first] && pre[e.first] > next)
                pre[e.first] = next;
        }
    }

    vector<int> trace;
    trace.push_back(s);
    while(trace.back() != g)
        trace.push_back(pre[trace.back()]);

    for (const int& e: trace) cout << e << ' ';
    cout << endl;
}
0