結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
けーむ
|
| 提出日時 | 2020-05-15 00:52:02 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,790 bytes |
| コンパイル時間 | 2,037 ms |
| コンパイル使用メモリ | 189,032 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-16 05:57:01 |
| 合計ジャッジ時間 | 3,352 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 8 WA * 18 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T>
struct Dijkstra {
const T inf = numeric_limits<T>::max() / 2 - 1;
vector<T> dist;
vector<int> path;
Dijkstra() {}
Dijkstra(int n) {
graph.resize(n);
dist.resize(n);
prev.resize(n);
}
void add_edge(int from, int to, T cost) {
graph[from].emplace_back(to, cost);
}
void get_distance(int from) {
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;
fill(dist.begin(), dist.end(), inf);
fill(prev.begin(), prev.end(), -1);
dist[from] = 0;
pq.push(make_pair(0, from));
while(!pq.empty()) {
pair<T, int> p = pq.top(); pq.pop();
if (dist[p.second] < p.first) continue;
for (auto e : graph[p.second]) {
if (
(dist[e.first] > (dist[p.second] + e.second)) ||
((dist[e.first] == (dist[p.second] + e.second)) && (prev[e.first] > p.second))
) {
dist[e.first] = dist[p.second] + e.second;
prev[e.first] = p.second;
pq.push(make_pair(dist[e.first], e.first));
}
}
}
}
void get_path(int to) {
path = vector<int>(0);
for (; to != -1; to = prev[to]) {
path.push_back(to);
}
reverse(path.begin(), path.end());
}
private:
vector<vector<pair<int, T>>> graph;
vector<T> prev;
};
struct S {
ll a, b, c;
bool operator<(const S &sb) const {
if (a == sb.a) return b < sb.b;
return a < sb.a;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// ifstream in("input.txt");
// cin.rdbuf(in.rdbuf());
ll n, m, s, g;
cin >> n >> m >> s >> g;
Dijkstra<ll> di(n);
vector<S> vs(2 * m);
rep(i, m) {
cin >> vs[i * 2].a >> vs[i * 2].b >> vs[i * 2].c;
vs[i * 2 + 1].a = vs[i * 2].b;
vs[i * 2 + 1].b = vs[i * 2].a;
vs[i * 2 + 1].c = vs[i * 2].c;
}
sort(all(vs));
rep(i, 2 * m) di.add_edge(vs[i].a, vs[i].b, vs[i].c);
di.get_distance(s);
di.get_path(g);
ll pl = sz(di.path);
rep(i, pl) printf("%lld%s", di.path[i], (i == (pl - 1)) ? "\n" : " ");
return 0;
}
けーむ