結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー |
|
提出日時 | 2020-10-24 18:43:01 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 7 ms / 5,000 ms |
コード長 | 1,392 bytes |
コンパイル時間 | 2,517 ms |
コンパイル使用メモリ | 207,096 KB |
最終ジャッジ日時 | 2025-01-15 15:12:52 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>; int main() { cin.tie(0);ios::sync_with_stdio(false); int N,M,S,G; cin >> N >> M >> S >> G; vector<vector<pair<int,int>>> edge(N); for(int i=0;i<M;++i){ int a,b,c; cin >> a >> b >> c; edge[a].push_back({b,c}); edge[b].push_back({a,c}); } for(int i=0;i<N;++i) sort(edge[i].begin(),edge[i].end()); int inf = 12345678; vector<int> dp(N,inf); priority_queue_reverse<pair<int,int>> pq; dp[G]=0; pq.push({0,G}); while(pq.size()) { auto p = pq.top(); pq.pop(); int from = p.second; if(p.first > dp[from]) continue; for(auto q:edge[from]) { int to = q.first; if(dp[to] > dp[from]+q.second) { dp[to] = dp[from]+q.second; pq.push({dp[to],to}); } } } vector<int> ans = {S}; int sum = 0; while(ans.back()!=G) { int from = ans.back(); for(auto q:edge[from]) { int to = q.first; if(sum + q.second + dp[to] == dp[S]) { ans.push_back(to); sum += q.second; break; } } } for(int i=0;i<ans.size();++i) cout << ans[i] << " \n"[i==ans.size()-1]; return 0; }