結果

問題 No.160 最短経路のうち辞書順最小
コンテスト
ユーザー Rumain831
提出日時 2026-09-21 16:53:15
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 16 ms / 5,000 ms
+ 48µs
コード長 922 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,771 ms
コンパイル使用メモリ 195,632 KB
実行使用メモリ 9,916 KB
最終ジャッジ日時 2026-09-21 16:53:25
合計ジャッジ時間 5,597 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
using P = pair<int, ll>;

int main(void){
  int n, m, s, g; cin >> n >> m >> s >> g; 
  vector dist(n, vector<ll>(n, 1e18));
  vector<vector<P>> to(n);
  for(int i=0; i<n; i++) dist[i][i]=0;
  while(m--){
    int a, b; ll c; cin >> a >> b >> c;
    to[a].emplace_back(b, c); dist[a][b]=c; swap(a, b);
    to[a].emplace_back(b, c); dist[a][b]=c; 
  }
  for(int i=0; i<n; i++) sort(begin(to[i]), end(to[i]));
  for(int k=0; k<n; k++)for(int i=0; i<n; i++)for(int j=0; j<n; j++){
    dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);
  }
  vector<int> ans;
  int now=s;
  ll d=dist[s][g];
  while(now!=g){
    ans.push_back(now);
    for(auto [p, c]:to[now]){
      if(dist[s][now]+c+dist[p][g]==d){
        now=p; break;
      }
    }
  } 
  ans.push_back(g);
  for(auto p:ans) cout << p << ' '; cout << endl;
  return 0; 
}
0