#include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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; Dijkstra(const vector > &graph_, const CostType CINF_ = LINF) : graph(graph_), CINF(CINF_) {} vector build(int s) { int sz = graph.size(); vector dist(sz, CINF); dist[s] = 0; prev.assign(sz, -1); priority_queue, greater > 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 > build2(int s) { int sz = graph.size(); vector > dist(sz, vector(2, CINF)); dist[s][false] = 0; using P = pair >; priority_queue, greater

> 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 build_path(int t) { vector res; for (; t != -1; t = prev[t]) res.emplace_back(t); reverse(ALL(res)); return res; } private: vector > graph; const CostType CINF; vector prev; }; int main() { cin.tie(0); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n, m; cin >> n >> m; vector > 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 iki = dij.build(0); vector > kaeri = dij.build2(0); REP(i, n) cout << iki[i] + min(kaeri[i][true], kaeri[i][false]) << '\n'; return 0; }