結果
問題 |
No.807 umg tours
|
ユーザー |
![]() |
提出日時 | 2019-03-22 21:59:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,825 bytes |
コンパイル時間 | 1,649 ms |
コンパイル使用メモリ | 137,004 KB |
最終ジャッジ日時 | 2025-01-06 23:49:59 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 14 WA * 12 |
ソースコード
#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 { 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); using Pci = pair<CostType, int>; 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<CostType> build2(int s) { int sz = graph.size(); vector<CostType> dist(sz, CINF); dist[s] = 0; prev.assign(sz, -1); using P = pair<CostType, pair<CostType, int> >; priority_queue<P, vector<P>, greater<P> > que; que.push({0, {0, s}}); while (!que.empty()) { P pr = que.top(); que.pop(); CostType di = pr.first, hiku = pr.second.first; int ver = pr.second.second; if (dist[ver] < di) continue; for (Edge e : graph[ver]) { if (e.cost > hiku) { CostType nx = di + hiku; if (dist[e.dst] > nx) { dist[e.dst] = nx; que.push({nx, {e.cost, e.dst}}); } } else { CostType nx = di + e.cost; if (dist[e.dst] > nx) { dist[e.dst] = nx; que.push({nx, {hiku, 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), kaeri = dij.build2(0); REP(i, n) cout << iki[i] + kaeri[i] << '\n'; return 0; }