結果

問題 No.160 最短経路のうち辞書順最小
ユーザー nmgm221nmgm221
提出日時 2017-04-19 01:07:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,583 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 93,480 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-26 13:29:55
合計ジャッジ時間 2,468 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 6 ms
4,380 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 3 ms
4,380 KB
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 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,384 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,384 KB
testcase_28 WA -
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[s] = 0;
    pre[s] = s;

    while(!used[g]) {
        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(g);
    while(trace.back() != s)
        trace.push_back(pre[trace.back()]);

    ritr(itr, trace)
        cout << *itr << ' ';
    cout << endl;
}
0