結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
|
提出日時 | 2016-06-21 17:56:37 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 20 ms / 5,000 ms |
コード長 | 1,660 bytes |
コンパイル時間 | 980 ms |
コンパイル使用メモリ | 90,180 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-11 18:28:47 |
合計ジャッジ時間 | 1,934 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#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; }