結果
問題 | No.614 壊れたキャンパス |
ユーザー |
![]() |
提出日時 | 2018-12-04 20:08:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 778 ms / 2,000 ms |
コード長 | 1,908 bytes |
コンパイル時間 | 2,235 ms |
コンパイル使用メモリ | 217,600 KB |
最終ジャッジ日時 | 2025-01-06 18:11:12 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#include<bits/stdc++.h>using namespace std;using Int = long long;template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}template<typename T>vector<T> compress(vector<T> v){sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());return v;}template<typename T>map<T, Int> dict(const vector<T> &v){map<T, Int> res;for(Int i=0;i<(Int)v.size();i++)res[v[i]]=i;return res;}template <typename T>vector<T> dijkstra(Int s,vector<vector<pair<Int, T> > > & G,T INF){using P = pair<T, Int>;Int n=G.size();vector<T> d(n,INF);vector<Int> b(n,-1);priority_queue<P,vector<P>,greater<P> > q;d[s]=0;q.emplace(d[s],s);while(!q.empty()){P p=q.top();q.pop();Int v=p.second;if(d[v]<p.first) continue;for(auto& e:G[v]){Int u=e.first;T c=e.second;if(d[u]>d[v]+c){d[u]=d[v]+c;b[u]=v;q.emplace(d[u],u);}}}return d;}//INSERT ABOVE HEREsigned main(){Int n,m,k,s,t;cin>>n>>m>>k>>s>>t;vector<Int> a(m),b(m),c(m);for(Int i=0;i<m;i++) cin>>a[i]>>b[i]>>c[i];using P = pair<Int, Int>;vector<P> vp;for(Int i=0;i<m;i++){a[i]--;vp.emplace_back(a[i],b[i]);vp.emplace_back(a[i]+1,c[i]);}vp.emplace_back(0,s);vp.emplace_back(n-1,t);vp=compress(vp);auto mp=dict(vp);Int sz=vp.size();const Int INF = 1e15;vector<vector<P> > G(sz);for(Int i=0;i<m;i++){Int x=mp[P(a[i],b[i])],y=mp[P(a[i]+1,c[i])];G[x].emplace_back(y,0);}for(Int i=0;i+1<sz;i++){if(vp[i].first!=vp[i+1].first) continue;Int d=vp[i+1].second-vp[i].second;G[i].emplace_back(i+1,d);G[i+1].emplace_back(i,d);}Int ans=dijkstra(mp[P(0,s)],G,INF)[mp[P(n-1,t)]];if(ans>=INF) ans=-1;cout<<ans<<endl;return 0;}