結果
問題 | No.807 umg tours |
ユーザー |
![]() |
提出日時 | 2019-03-23 02:18:12 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 295 ms / 4,000 ms |
コード長 | 3,935 bytes |
コンパイル時間 | 1,550 ms |
コンパイル使用メモリ | 140,036 KB |
最終ジャッジ日時 | 2025-01-07 00:08:14 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <algorithm> #include <cassert> #include <cctype> #include <chrono> #define _USE_MATH_DEFINES #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <vector> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const int MOD = 1000000007; // 998244353 const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; /*-------------------------------------------------*/ using CostType = long long; struct Edge { int src, dst; CostType cost; Edge(int src_, int dst_, CostType cost_ = 0) : src(src_), dst(dst_), cost(cost_) {} inline bool operator<(const Edge &rhs) const { return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src; } inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; } inline bool operator>(const Edge &rhs) const { return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src; } inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; } }; struct Dijkstra { using Pci = pair<CostType, int>; Dijkstra(const vector<vector<Edge> > &graph_, const CostType CINF_ = LINF) : graph(graph_), CINF(CINF_) {} vector<CostType> build(int s) { int sz = graph.size(); vector<CostType> dist(sz, CINF); dist[s] = 0; prev.assign(sz, -1); priority_queue<Pci, vector<Pci>, greater<Pci> > que; que.emplace(0, s); while (!que.empty()) { Pci pr = que.top(); que.pop(); int ver = pr.second; if (dist[ver] < pr.first) continue; for (Edge e : graph[ver]) { if (dist[e.dst] > dist[ver] + e.cost) { dist[e.dst] = dist[ver] + e.cost; prev[e.dst] = ver; que.emplace(dist[e.dst], e.dst); } } } return dist; } vector<vector<CostType> > build2(int s) { int sz = graph.size(); vector<vector<CostType> > dist(sz, vector<CostType>(2, CINF)); dist[s][false] = 0; using P = pair<CostType, pair<bool, int> >; priority_queue<P, vector<P>, greater<P> > que; que.push({0, {false, 0}}); while (!que.empty()) { P p = que.top(); que.pop(); CostType cost = p.first; bool used = p.second.first; int ver = p.second.second; if (dist[ver][used] < cost) continue; for (Edge e : graph[ver]) { if (dist[e.dst][used] > dist[ver][used] + e.cost) { dist[e.dst][used] = dist[ver][used] + e.cost; que.push({dist[e.dst][used], {used, e.dst}}); } if (!used) { if (dist[e.dst][true] > dist[ver][used]) { dist[e.dst][true] = dist[ver][used]; que.push({dist[e.dst][true], {true, e.dst}}); } } } } return dist; } vector<int> build_path(int t) { vector<int> res; for (; t != -1; t = prev[t]) res.emplace_back(t); reverse(ALL(res)); return res; } private: vector<vector<Edge> > graph; const CostType CINF; vector<int> prev; }; int main() { cin.tie(0); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n, m; cin >> n >> m; vector<vector<Edge> > graph(n); while (m--) { int a, b, c; cin >> a >> b >> c; --a; --b; graph[a].emplace_back(Edge(a, b, c)); graph[b].emplace_back(Edge(b, a, c)); } Dijkstra dij(graph); vector<long long> iki = dij.build(0); vector<vector<long long> > kaeri = dij.build2(0); REP(i, n) cout << iki[i] + min(kaeri[i][true], kaeri[i][false]) << '\n'; return 0; }