結果
問題 |
No.848 なかよし旅行
|
ユーザー |
|
提出日時 | 2019-07-07 18:54:30 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 159 ms / 2,000 ms |
コード長 | 1,248 bytes |
コンパイル時間 | 1,045 ms |
コンパイル使用メモリ | 87,384 KB |
実行使用メモリ | 10,752 KB |
最終ジャッジ日時 | 2024-11-08 00:58:43 |
合計ジャッジ時間 | 3,139 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
コンパイルメッセージ
main.cpp:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 37 | main() | ^~~~
ソースコード
#include<iostream> #include<vector> #include<algorithm> using namespace std; #include<vector> #include<queue> #include<limits> template<typename T> vector<T>dijkstra(int s,const vector<vector<pair<int,T> > >&G,T INF=numeric_limits<T>::max()) { int n=G.size(); vector<T>d(n,INF); vector<int>parent(n,-1); priority_queue<pair<T,int>,vector<pair<T,int> >,greater<pair<T,int> > >P; d[s]=0; P.push(make_pair(d[s],s)); while(!P.empty()) { pair<T,int>p=P.top();P.pop(); int v=p.second; if(d[v]<p.first)continue; for(const pair<int,T>&e:G[v]) { int u=e.first; T cost=d[v]+e.second; if(d[u]>cost) { d[u]=cost; parent[u]=v; P.push(make_pair(d[u],u)); } } } return d; } int N,M,P,Q,T; main() { cin>>N>>M>>P>>Q>>T; vector<vector<pair<int,long> > >G(N); for(int i=0;i<M;i++) { int a,b,c;cin>>a>>b>>c; a--,b--; G[a].push_back(make_pair(b,c)); G[b].push_back(make_pair(a,c)); } P--,Q--; vector<long>d1=dijkstra(0,G); vector<long>dp=dijkstra(P,G); vector<long>dq=dijkstra(Q,G); long ans=-1; if(d1[P]+dp[Q]+dq[0]<=T)ans=T; for(int i=0;i<N;i++)for(int j=0;j<N;j++) { if(d1[i]+max(dp[i]+dp[j],dq[i]+dq[j])+d1[j]<=T) { ans=max(ans,T-max(dp[i]+dp[j],dq[i]+dq[j])); } } cout<<ans<<endl; }