結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2015-03-01 23:58:18 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,422 bytes |
コンパイル時間 | 717 ms |
コンパイル使用メモリ | 62,944 KB |
実行使用メモリ | 814,132 KB |
最終ジャッジ日時 | 2024-06-24 00:43:07 |
合計ジャッジ時間 | 6,677 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 3 MLE * 1 -- * 22 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:34:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 34 | scanf("%d%d%d%d",&n,&m,&s,&g); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:37:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 37 | scanf("%d%d%d",&a,&b,&c); | ~~~~~^~~~~~~~~~~~~~~~~~~
ソースコード
#include<cstdio> #include<algorithm> #include<vector> #include<queue> #include<set> using namespace std; struct edge{int to,cost;}; struct data{ int pos,cost; bool operator>(const data &d)const{ return cost>d.cost; } }; const int INF=1e9+7; int n,m,s,g; vector<edge>G[200]; int d[200]; set<vector<int> >S; void dfs(int pos,int cost,vector<int>way){ way.push_back(pos); if(pos==s){ reverse(way.begin(),way.end()); S.insert(way); return; } for(int i=0;i<G[pos].size();i++){ edge &e=G[pos][i]; if(d[e.to]==cost-e.cost){ dfs(e.to,cost-e.cost,way); } } } int main(){ scanf("%d%d%d%d",&n,&m,&s,&g); for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); G[a].push_back((edge){b,c}); G[b].push_back((edge){a,c}); } fill_n(d,n,INF); priority_queue<data,vector<data>,greater<data> >Q; Q.push((data){s,0}); while(Q.size()){ int pos=Q.top().pos,cost=Q.top().cost; Q.pop(); if(d[pos]<cost)continue; d[pos]=cost; for(int i=0;i<G[pos].size();i++){ edge &e=G[pos][i]; Q.push((data){e.to,cost+e.cost}); } } dfs(g,d[g],vector<int>(0)); vector<int>ans=*(S.begin()); for(int i=0;i<ans.size();i++){ if(i)printf(" "); printf("%d",ans[i]); } puts(""); return 0; }