結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2019-03-22 22:11:45 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 186 ms / 4,000 ms |
| コード長 | 1,420 bytes |
| コンパイル時間 | 1,902 ms |
| コンパイル使用メモリ | 182,668 KB |
| 実行使用メモリ | 16,332 KB |
| 最終ジャッジ日時 | 2024-11-23 19:02:12 |
| 合計ジャッジ時間 | 5,486 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
struct Edge { int to, cost; };
template<class T, class Edge> pair< V<T>, V<T> > dijkstra(const VV<Edge>& g, int s = 0) {
V<T> dist(g.size(), numeric_limits<T>::max());
using P = pair<T, int>;
priority_queue< P, V<P>, greater<P> > pque;
pque.emplace(dist[s] = 0, s);
while (!pque.empty()) {
T d; int v;
tie(d, v) = pque.top(); pque.pop();
if (d > dist[v]) continue;
for (const auto& e : g[v]) if (dist[v] + e.cost < dist[e.to]) {
pque.emplace(dist[e.to] = dist[v] + e.cost, e.to);
}
}
auto dp = dist;
decltype(pque)().swap(pque);
pque.emplace(dp[s] = 0, s);
while (!pque.empty()) {
T d; int v;
tie(d, v) = pque.top(); pque.pop();
if (d > dp[v]) continue;
for (const auto& e : g[v]) {
auto x = min(dist[v], dp[v] + e.cost);
if (x < dp[e.to]) {
pque.emplace(dp[e.to] = x, e.to);
}
}
}
return {dist, dp};
}
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
int n, m; cin >> n >> m;
VV<Edge> g(n);
while (m--) {
int a, b, c; cin >> a >> b >> c, --a, --b;
g[a].emplace_back(Edge{b, c});
g[b].emplace_back(Edge{a, c});
}
auto p = dijkstra<lint>(g);
for (int i = 0; i < n; ++i) {
cout << p.first[i] + p.second[i] << '\n';
}
}
risujiroh