結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-05-24 20:00:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 2,139 bytes
コンパイル時間 2,494 ms
コンパイル使用メモリ 159,668 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-02 09:24:41
合計ジャッジ時間 3,108 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.05.24 20:00:07
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

// dijkstra O(ElogV)
// verify : https://onlinejudge.u-aizu.ac.jp/problems/GRL_1_A
// ※拡張ダイクストラ
template<typename T>
struct Dijkstra {
private:
    int V;
    struct edge { int to; T cost; };
public:
    vector<vector<edge>> G;
    const T inf = numeric_limits<T>::max();
    // s から i の最小コスト
    // 経路がない場合は inf
    vector<T> d; // (頂点) ※
    Dijkstra(int V) : V(V) {
        G.resize(V);
    }
    // 辺の追加
    // 有向の場合 directed = true
    void add_edge(int from, int to, T weight, bool directed = false) {
        G[from].push_back({to,weight});
        if (!directed) G[to].push_back({from,weight});
    }
    void build(int s) {
        d.assign(V, inf); // ※
        typedef tuple<T, int> P; //(距離,頂点) ※
        priority_queue<P, vector<P>, greater<P>> pq;
        d[s] = 0; // ※
        pq.push(P(d[s], s)); // ※
        while (!pq.empty()) {
            P p = pq.top(); pq.pop();
            int v = get<1>(p);
            // ※
            if (d[v] < get<0>(p)) continue; // ※
            for (const edge &e : G[v])
            {
                // ※
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    pq.push(P(d[e.to], e.to));
                }
            }
        }
    }
};
int main() {
    int n, m;
    cin >> n >> m;
    int sv, gv;
    cin >> sv >> gv;
    Dijkstra<int> g(n);
    for (int i = 0; i < m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        g.add_edge(u,v,c);
    }
    g.build(gv);
    vector<int> ans;
    int v = sv;
    ans.push_back(v);
    while(v != gv) {
        int u = n;
        for(auto e : g.G[v]) {
            if (g.d[e.to] + e.cost == g.d[v]) {
                u = min(u,e.to);
            } 
        }
        v = u;
        ans.push_back(v);
    }
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
    return 0;
}
0