結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2015-03-02 14:51:58 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 19 ms / 5,000 ms |
コード長 | 1,520 bytes |
コンパイル時間 | 852 ms |
コンパイル使用メモリ | 92,824 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-24 01:11:32 |
合計ジャッジ時間 | 1,850 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include<iostream> #include<cstdio> #include<algorithm> #include<climits> #include<string> #include<vector> #include<list> #include<map> #include<set> #include<cmath> #include<queue> #include<cstring> #include<stack> using namespace std; struct Way{ int to,cost; Way(){} Way(int _to,int _cost){ to=_to; cost=_cost; } }; struct Data{ int n,cost; Data(){} Data(int _n,int _cost){ n=_n; cost=_cost; } bool operator<(const Data &a)const{ return cost>a.cost; } }; int N,M,S,G,A,B,C; vector<Way> way[200]; int memo[200],d[200]; void dfs(int n,vector<int> ans){ if(memo[n]==true) return; memo[n] = true; ans.push_back(n); if(n==G){ printf("%d",ans[0]); for(int i=1;i<ans.size();i++) printf(" %d",ans[i]); puts(""); } vector<int> way_; for(int i=0;i<way[n].size();i++){ if(d[n]+way[n][i].cost==d[way[n][i].to]){ way_.push_back(way[n][i].to); } } sort(way_.begin(),way_.end()); for(int i=0;i<way_.size();i++) dfs(way_[i],ans); } int main(){ cin>>N>>M>>S>>G; for(int i=0;i<M;i++){ cin>>A>>B>>C; way[A].push_back(Way(B,C)); way[B].push_back(Way(A,C)); } priority_queue<Data> pq; pq.push(Data(S,0)); Data pq_c; fill_n((int*)d,200,INT_MAX); while(!pq.empty()){ pq_c = pq.top(); pq.pop(); if(d[pq_c.n]!=INT_MAX) continue; d[pq_c.n] = pq_c.cost; for(int i=0;i<way[pq_c.n].size();i++){ pq.push(Data(way[pq_c.n][i].to,pq_c.cost+way[pq_c.n][i].cost)); } } vector<int> ans; dfs(S,ans); return 0; }