結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2015-03-02 03:09:16 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,271 bytes |
コンパイル時間 | 801 ms |
コンパイル使用メモリ | 90,248 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-24 01:00:59 |
合計ジャッジ時間 | 1,778 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 8 WA * 18 |
ソースコード
#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,prev; Data(){} Data(int _n,int _cost,int _prev){ n=_n; cost=_cost; prev=_prev; } bool operator<(const Data &a)const{ if(cost==a.cost) return prev>a.prev; return cost>a.cost; } }; int main(){ vector<Way> way[200]; int N,M,S,G,A,B,C; 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,-1)); Data pq_c; bool used[200]={}; int prev[200]; while(!pq.empty()){ pq_c = pq.top(); pq.pop(); if(used[pq_c.n]==true) continue; used[pq_c.n] = true; prev[pq_c.n] = pq_c.prev; 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,pq_c.n)); } } stack<int> s; int n=G; s.push(n); while(n!=S){ n = prev[n]; s.push(n); } printf("%d",s.top()); s.pop(); while(!s.empty()){ printf(" %d",s.top()); s.pop(); }puts(""); return 0; }