結果

問題 No.160 最短経路のうち辞書順最小
ユーザー kimiyuki
提出日時 2016-06-21 17:47:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,617 bytes
コンパイル時間 1,153 ms
コンパイル使用メモリ 87,612 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-11 18:25:50
合計ジャッジ時間 2,160 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 8 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#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<int> from(n, inf);
    priority_queue<state_t> que; // dijkstra
    que.push((state_t) { start, 0 });
    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 {
                // to reconstruct the path
                if (dist[e.to] + e.cost == dist[e.from]) {
                    setmin(from[e.from], e.to);
                }
            }
        }
    }
    // reconstruct
    deque<int> path;
    for (int v = goal; v != inf; v = from[v]) {
        path.push_front(v);
    }
    // output
    for (int v : path) cout << v << ' ';
    cout << endl;
    return 0;
}
0