結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2017-04-19 01:07:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,583 bytes |
コンパイル時間 | 879 ms |
コンパイル使用メモリ | 100,472 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-19 07:50:52 |
合計ジャッジ時間 | 2,240 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 8 WA * 18 |
ソースコード
#include <iostream> #include <vector> #include <map> #include <algorithm> #include <string> #include <cmath> #include <stack> #include <queue> #include <list> #include <set> #include <numeric> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() #define itr(i, x) for(auto i = (x).begin(); i != (x).end(); i++) #define ritr(i, x) for(auto i = (x).rbegin(); i != (x).rend(); i++) #define INF 1010101010 #define MOD 1000000007 #define LL long long int main() { int n, m, s, g; cin >> n >> m >> s >> g; vector<vector<pair<int, int>>> gr(n); vector<int> mdist(n, INF); vector<bool> used(n, false); vector<int> pre(n); rep(i, m) { int a, b, c; cin >> a >> b >> c; gr[a].push_back(make_pair(b, c)); gr[b].push_back(make_pair(a, c)); } mdist[s] = 0; pre[s] = s; while(!used[g]) { int next = -1; rep(i, n) next = !used[i] && (next == -1 || mdist[i] < mdist[next]) ? i : next; used[next] = true; for (const auto e: gr[next]) { if (mdist[next] + e.second < mdist[e.first]) { pre[e.first] = next; mdist[e.first] = mdist[next] + e.second; } if (mdist[next] + e.second == mdist[e.first] && pre[e.first] > next) pre[e.first] = next; } } vector<int> trace; trace.push_back(g); while(trace.back() != s) trace.push_back(pre[trace.back()]); ritr(itr, trace) cout << *itr << ' '; cout << endl; }