結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー |
![]() |
提出日時 | 2021-12-23 20:00:12 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 20 ms / 5,000 ms |
コード長 | 1,616 bytes |
コンパイル時間 | 2,137 ms |
コンパイル使用メモリ | 145,548 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-17 18:40:37 |
合計ジャッジ時間 | 3,472 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <cassert> #include <cmath> #include <algorithm> #include <iostream> #include <iomanip> #include <climits> #include <map> #include <queue> #include <set> #include <cstring> #include <vector> using namespace std; typedef long long ll; struct Node { int v; int parent; ll cost; Node(int v = -1, int parent = -1, ll cost = -1) { this->v = v; this->parent = parent; this->cost = cost; } bool operator>(const Node &n) const { return cost > n.cost; } }; struct Edge { int u; ll cost; Edge(int u, ll cost) { this->u = u; this->cost = cost; } }; vector<Edge> E[210]; int main() { int N, M, S, G; cin >> N >> M >> S >> G; for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; E[a].push_back(Edge(b, c)); E[b].push_back(Edge(a, c)); } vector<ll> dist(N + 1, LLONG_MAX); priority_queue <Node, vector<Node>, greater<Node>> pque; pque.push(Node(G, -1, 0)); while (not pque.empty()) { Node node = pque.top(); pque.pop(); if (dist[node.v] <= node.cost) continue; dist[node.v] = node.cost; for (auto e : E[node.v]) { ll ncost = node.cost + e.cost; pque.push(Node(e.u, node.v, ncost)); } } vector<int> ans; int cur = S; ans.push_back(cur); while (true) { int next = 1 << 29; for (Edge &e : E[cur]) { if (dist[cur] == dist[e.u] + e.cost) { next = min(next, e.u); } } cur = next; ans.push_back(cur); if (cur == G) break; } for (int v : ans) { cout << v; if (v != ans.back()) cout << " "; } cout << endl; return 0; }