結果

問題 No.160 最短経路のうち辞書順最小
ユーザー pekempeypekempey
提出日時 2015-08-18 21:07:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,540 bytes
コンパイル時間 1,271 ms
コンパイル使用メモリ 155,196 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 12:55:33
合計ジャッジ時間 2,607 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

struct edge { int to, cost; };
typedef pair<int, int> P;
vector<edge> G[200];
int dp[200];

ostream &operator <<(ostream &os, const vector<int> &v) {
    rep (i, v.size()) {
        if (i) cout << " ";
        cout << v[i];
    }
    return os;
}

int main() {
    int N, M, s, t;
    cin >> N >> M >> s >> t;
    rep (i, M) {
        int a, b, c;
        cin >> a >> b >> c;
        G[a].push_back((edge){b, c});
        G[b].push_back((edge){a, c});
    }

    priority_queue<P, vector<P>, greater<P>> q;
    q.emplace(0, t);
    rep (i, N) dp[i] = inf;
    dp[t] = 0;

    while (!q.empty()) {
        P p = q.top(); q.pop();
        int v = p.second;
        
        for (edge e : G[v]) {
            if (dp[e.to] > dp[v] + e.cost) {
                dp[e.to] = dp[v] + e.cost;
                q.emplace(dp[e.to], e.to);
            }
        } 
    }

    int curr = s;
    vector<int> ans;
    ans.push_back(s);

    while (curr != t) {
        int minv = inf;
        for (edge e : G[curr]) {
            if (dp[curr] == dp[e.to] + e.cost) {
                minv = min(minv, e.to);
            }
        }
        curr = minv;
        ans.push_back(curr);
    }

    cout << ans << endl;

    return 0;
}
0