結果
問題 | No.807 umg tours |
ユーザー | mamekin |
提出日時 | 2019-03-22 22:47:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 443 ms / 4,000 ms |
コード長 | 1,920 bytes |
コンパイル時間 | 1,533 ms |
コンパイル使用メモリ | 134,864 KB |
実行使用メモリ | 18,392 KB |
最終ジャッジ日時 | 2024-11-23 19:06:29 |
合計ジャッジ時間 | 7,647 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <array> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> #include <memory> #include <regex> using namespace std; class Edge { public: int to, cost; Edge(int to, int cost){ this->to = to; this->cost = cost; } }; const long long INF = LLONG_MAX / 2; int main() { int n, m; cin >> n >> m; vector<vector<Edge> > edges(n); for(int i=0; i<m; ++i){ int a, b, c; cin >> a >> b >> c; -- a; -- b; edges[a].push_back(Edge(b, c)); edges[b].push_back(Edge(a, c)); } vector<vector<long long> > dp(2, vector<long long>(n, INF)); dp[0][0] = dp[1][0] = 0; priority_queue<tuple<long long, int, int> > pq; pq.push(make_tuple(-0, 0, 0)); pq.push(make_tuple(-0, 1, 0)); while(!pq.empty()){ long long cost; int cnt, curr; tie(cost, cnt, curr) = pq.top(); cost *= -1; pq.pop(); if(dp[cnt][curr] < cost) continue; for(const Edge& e : edges[curr]){ int next = e.to; if(cnt == 0 && cost < dp[1][next]){ dp[1][next] = cost; pq.push(make_tuple(-cost, 1, next)); } long long cost2 = cost + e.cost; if(cost2 < dp[cnt][next]){ dp[cnt][next] = cost2; pq.push(make_tuple(-cost2, cnt, next)); } } } for(int i=0; i<n; ++i){ long long ans = dp[0][i] + dp[1][i]; cout << ans << endl; } return 0; }