結果

問題 No.160 最短経路のうち辞書順最小
ユーザー izryt(趣味)izryt(趣味)
提出日時 2017-01-30 10:28:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 1,565 bytes
コンパイル時間 1,693 ms
コンパイル使用メモリ 177,032 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-25 15:13:57
合計ジャッジ時間 3,398 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 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,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,380 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,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 13 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define rep(i,x) for(int i=0;i<x;++i)
#define rep1(i,x) for(int i=1;i<=x;++i)
#define rrep(i,x) for(int i=x-1;i>=0;--i)
#define rrep1(i,x) for(int i=x;i>=1;--i)
#define all(a) begin(a),end(a)
#define fst first
#define scd second
#define PB push_back

const int inf = 1e9;
const int mod = 1e9 + 7;

using pii=pair<int,short>;
using vpii=vector<pii>;

struct edge{short to;int cost;};
vector<edge> G[1005];

int N, M, s, g;
pair<int,short> pre[1005];

int d[1005];

signed main()
{
    cin >> N >> M >> s >> g;

    rep(i, M) {
        short a, b; int c; cin >> a >> b >> c;
        G[a].PB(edge{b, c});
        G[b].PB(edge{a, c});
    }

    rep(i, N) {
        pre[i] = pii(inf, N+4);
    }

    priority_queue<pii, vpii, greater<pii>> q;
    q.push(pii(0, g));
    fill_n(d, N+1, inf);
    d[g] = 0;

    while (q.size()) {
        pii p = q.top(); q.pop();

        short v = p.scd; int c = p.fst;

        if (d[v] < c) continue;

        for (edge &e : G[v]) {
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                q.push(pii(d[e.to], e.to));
            }
        }
    }

    vector<int> ans;

    short cur = s;
    ans.PB(s);
    while (cur != g) {
        short to = N+4;

        for (edge &e : G[cur]) {
            if (d[e.to] == d[cur] - e.cost) {
                to = min(to, e.to);
            }
        }

        ans.PB(to);

        cur = to;
    }

    rep(i, ans.size()) {
        if (i) cout << ' ';
        cout << ans[i];
    }
    cout<<endl;
}

0