結果
問題 | No.807 umg tours |
ユーザー |
![]() |
提出日時 | 2019-03-22 23:59:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 464 ms / 4,000 ms |
コード長 | 1,409 bytes |
コンパイル時間 | 1,787 ms |
コンパイル使用メモリ | 179,980 KB |
実行使用メモリ | 23,760 KB |
最終ジャッジ日時 | 2024-11-23 19:14:18 |
合計ジャッジ時間 | 7,969 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include "bits/stdc++.h" using namespace std; using P = pair<long, int>; struct edge {int to; long c;}; void solve() { int n, m; cin >> n >> m; vector<vector<long>> dist(2, vector<long>(n, 1e18)); dist[0][0] = dist[1][0] = 0; vector<edge> g[n]; while(m--) { int a, b; long c; cin >> a >> b >> c; a--, b--; g[a].push_back({b, c}); g[b].push_back({a, c}); } priority_queue<P, vector<P>, greater<P>> q, q2; q.push({0, 0}); q2.push({0, 0}); while(!q.empty() || !q2.empty()) { while (!q.empty()) { auto p = q.top(); q.pop(); int v = p.second; long c = p.first; if (dist[0][v] < c) continue; for (auto np : g[v]) { int nv = np.to; long nc = np.c + dist[0][v]; if (dist[0][nv] > nc) { dist[0][nv] = nc; q.push({nc, nv}); } if (dist[1][nv] > dist[0][v]) { dist[1][nv] = dist[0][v]; q2.push({dist[0][v], nv}); } } } while(!q2.empty()) { auto p = q2.top(); q2.pop(); int v = p.second; long c = p.first; if (dist[1][v] < c) continue; for (auto np : g[v]) { int nv = np.to; long nc = np.c + dist[1][v]; if (dist[1][nv] > nc) { dist[1][nv] = nc; q2.push({nc, nv}); } } } } for (int i = 0; i < n; i++) { long ans = dist[0][i] + dist[1][i]; cout << ans << endl; } } int main(void) { solve(); //cout << "yui(*-v・)yui" << endl; return 0; }