結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
nmgm221
|
| 提出日時 | 2017-04-19 01:31:22 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 14 ms / 5,000 ms |
| コード長 | 1,581 bytes |
| コンパイル時間 | 934 ms |
| コンパイル使用メモリ | 99,428 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-19 07:51:10 |
| 合計ジャッジ時間 | 1,797 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#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[g] = 0;
pre[g] = g;
while(!used[s]) {
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(s);
while(trace.back() != g)
trace.push_back(pre[trace.back()]);
for (const int& e: trace) cout << e << ' ';
cout << endl;
}
nmgm221