結果

問題 No.160 最短経路のうち辞書順最小
ユーザー kimiyukikimiyuki
提出日時 2016-06-21 17:56:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,660 bytes
コンパイル時間 1,073 ms
コンパイル使用メモリ 89,364 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 23:43:15
合計ジャッジ時間 1,904 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
template <class T> bool setmin(T & l, T const & r) { if (not (r < l)) return false; l = r; return true; }
using namespace std;

struct edge_t { int from, to, cost; };
struct state_t { int v; int cost; };
bool operator < (state_t a, state_t b) { return a.cost > b.cost; } // strict weak ordering
const int inf = 1e9+7;
int main() {
    // input
    int n, m, start, goal; cin >> n >> m >> start >> goal;
    vector<vector<edge_t> > g(n);
    repeat (i,m) {
        edge_t e; cin >> e.from >> e.to >> e.cost;
        g[e.from].push_back(e);
        swap(e.from, e.to);
        g[e.from].push_back(e);
    }
    // search
    vector<int> dist(n, inf);
    vector<vector<int> > path(n, vector<int>({ inf }));
    priority_queue<state_t> que; // dijkstra
    que.push((state_t) { start, 0 });
    path[start] = vector<int>({ start });
    while (not que.empty()) {
        state_t s = que.top(); que.pop();
        if (dist[s.v] != inf) continue;
        dist[s.v] = s.cost;
        for (auto e : g[s.v]) {
            if (dist[e.to] == inf) {
                que.push((state_t) { e.to, dist[e.from] + e.cost });
            } else {
                if (dist[e.to] + e.cost == dist[e.from]) {
                    vector<int> npath = path[e.to];
                    npath.push_back(e.from);
                    setmin(path[e.from], npath);
                }
            }
        }
    }
    // output
    repeat (i,path[goal].size()) {
        if (i) cout << ' ';
        cout << path[goal][i];
    }
    cout << endl;
    return 0;
}
0