結果

問題 No.614 壊れたキャンパス
ユーザー beetbeet
提出日時 2018-12-04 20:08:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 969 ms / 2,000 ms
コード長 1,908 bytes
コンパイル時間 2,798 ms
コンパイル使用メモリ 223,976 KB
実行使用メモリ 81,340 KB
最終ジャッジ日時 2023-09-22 07:11:31
合計ジャッジ時間 13,342 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 839 ms
79,632 KB
testcase_09 AC 938 ms
78,780 KB
testcase_10 AC 791 ms
78,268 KB
testcase_11 AC 910 ms
81,112 KB
testcase_12 AC 969 ms
80,776 KB
testcase_13 AC 909 ms
81,340 KB
testcase_14 AC 845 ms
81,048 KB
testcase_15 AC 725 ms
78,900 KB
testcase_16 AC 746 ms
79,304 KB
testcase_17 AC 494 ms
70,132 KB
testcase_18 AC 430 ms
67,024 KB
testcase_19 AC 429 ms
64,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 HERE
signed 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;
}
0