結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2022-12-25 19:02:05 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 33 ms / 5,000 ms |
コード長 | 1,385 bytes |
コンパイル時間 | 1,174 ms |
コンパイル使用メモリ | 118,756 KB |
最終ジャッジ日時 | 2025-02-09 20:57:06 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <iostream> #include <vector> #include <cmath> #include <map> #include <set> #include <iomanip> #include <queue> #include <algorithm> using namespace std; template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>; int main(){ long long N, M, A, B, C, S, G; long long from, alt, d; cin >> N >> M >> S >> G; pq<pair<long long, long long>> que; vector<vector<pair<long long, long long>>> E(N); vector<long long> dist(N, 1e18), ans; vector<bool> visit(N); for (int i=0; i < M; i++){ cin >> A >> B >> C; E[A].push_back({B, C}); E[B].push_back({A, C}); } for (auto &x : E) sort(x.begin(), x.end()); dist[G] = 0; que.push({0, G}); while(!que.empty()){ tie(d, from) = que.top(); que.pop(); if (visit[from]) continue; visit[from] = 1; for (auto [to, C] : E[from]){ alt = d + C; if (alt < dist[to]){ dist[to] = alt; que.push({dist[to], to}); } } } ans.push_back(S); long long now = S; while(now != G){ for (auto [x, y] : E[now]){ if (dist[x] == dist[now] - y){ now = x; ans.push_back(now); } } } for (auto x : ans) cout << x << " "; cout << endl; return 0; }