結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
|
提出日時 | 2020-05-02 12:49:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 20 ms / 5,000 ms |
コード長 | 1,234 bytes |
コンパイル時間 | 2,080 ms |
コンパイル使用メモリ | 171,476 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-29 17:19:08 |
合計ジャッジ時間 | 3,886 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (int)n; ++i) #define FOR(i, a, b) for(int i = a; i < (int)b; ++i) #define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i) typedef long long ll; typedef long double ld; const int Inf = 1e9; const double EPS = 1e-9; const int MOD = 1e9 + 7; int main() { cin.tie(nullptr); ios::sync_with_stdio(0); int n, m, s, g; cin >> n >> m >> s >> g; vector<vector<int> > d(n, vector<int>(n, Inf)); vector<vector<int> > next(n, vector<int>(n)); rep (i, n) { rep (j, n) next[i][j] = j; d[i][i] = 0; } rep (i, m) { int a, b, c; cin >> a >> b >> c; d[a][b] = d[b][a] = c; } rep (k, n) { rep (i, n) { rep (j, n) { if (d[i][j] > d[i][k] + d[k][j]) { d[i][j] = d[i][k] + d[k][j]; next[i][j] = next[i][k]; } else if (k != i && d[i][k] + d[k][j] == d[i][j]) { next[i][j] = min(next[i][j], next[i][k]); } } } } for (int x = s; x != g; x = next[x][g]) { cout << x << " "; } cout << g << endl; return 0; }