結果

問題 No.160 最短経路のうち辞書順最小
ユーザー beet
提出日時 2018-11-14 11:43:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 1,342 bytes
コンパイル時間 2,439 ms
コンパイル使用メモリ 205,576 KB
最終ジャッジ日時 2025-01-06 16:37:59
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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> 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,s,g;
  cin>>n>>m>>s>>g;
  using P = pair<Int, Int>;
  vector<vector<P> > G(n);
  for(Int i=0;i<m;i++){
    Int a,b,c;
    cin>>a>>b>>c;
    G[a].emplace_back(b,c);
    G[b].emplace_back(a,c);
  }
  const Int INF = 1e9;
  auto ds=dijkstra(s,G,INF);
  auto dg=dijkstra(g,G,INF);

  Int cur=s;
  cout<<cur;
  while(cur!=g){
    Int nxt=-1;
    for(P e:G[cur]){
      Int u,c;
      tie(u,c)=e;
      if(ds[cur]+c+dg[u]!=ds[g]) continue;
      if(nxt<0||u<nxt) nxt=u;
    }
    cur=nxt;
    cout<<" "<<cur;
  }
  cout<<endl;
  return 0;
}
0