結果
問題 |
No.17 2つの地点に泊まりたい
|
ユーザー |
![]() |
提出日時 | 2015-06-26 10:18:56 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,443 bytes |
コンパイル時間 | 973 ms |
コンパイル使用メモリ | 99,244 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-07 17:40:50 |
合計ジャッジ時間 | 1,679 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 7 WA * 20 |
ソースコード
#include <iostream> #include <string> #include <vector> #include <cmath> #include <algorithm> #include <cstdlib> #include <ctime> #include <cstdio> #include <functional> #include <set> #include <sstream> #include <map> #include <queue> #include <stack> using namespace std; struct edge{int to,cost;}; typedef pair<int,int> P; typedef pair<int,P> PP; vector<vector<edge> > G; int main() { int n; cin>>n; G.resize(n); vector<int> stay(n); for(int i=0;i<n;i++) cin>>stay[i]; int m; cin>>m; for(int i=0;i<m;i++){ int a,b,c; cin>>a>>b>>c; G[a].push_back(edge{b,c}); G[b].push_back(edge{a,c}); } //d[今の地点][これまで何回泊まったか]=費用 vector<vector<int> > d(n,vector<int>(3,1000000)); priority_queue<PP,vector<PP>,greater<PP> > q; d[0][0]=0; q.push(PP(0,P(0,0))); vector<bool> stayed(n,false); while(!q.empty()){ PP pp=q.top(); q.pop(); int now=pp.second.first; int cnt=pp.second.second; if(pp.first>d[now][cnt]) continue; for(int i=0;i<G[now].size();i++){ int to=G[now][i].to; int cost=G[now][i].cost; if(d[to][cnt]>d[now][cnt]+cost){ d[to][cnt]=d[now][cnt]+cost; q.push(PP(d[to][cnt],P(to,cnt))); } } if(cnt<2 && !stayed[now] && now!=0 && now!=n-1 && d[now][cnt+1]>d[now][cnt]+stay[now]){ stayed[now]=true; d[now][cnt+1]=d[now][cnt]+stay[now]; q.push(PP(d[now][cnt+1],P(now,cnt+1))); } } cout<<d[n-1][2]<<endl; return 0; }