// IO library #include #include #include #include // algorithm library #include #include #include #include // contancer library #include #include #include #include #include #include #include #include #include #include using ll = long long; using ld = long double; /* ----- Output Functions for Debugging ----- */ template std::ostream& operator<<(std::ostream& os, std::vector v); template std::ostream& operator<<(std::ostream& os, std::set v); template std::ostream& operator<<(std::ostream& os, std::pair p); template std::ostream& operator<<(std::ostream& os, std::map v); template std::ostream& operator<<(std::ostream& os, std::queue q); template std::ostream& operator<<(std::ostream& os, std::priority_queue q); template std::ostream& operator<<(std::ostream& os, std::vector v) { os << "["; for (auto vv : v) os << vv << ","; return os << "]"; } template std::ostream& operator<<(std::ostream& os, std::set v) { os << "{"; for (auto vv : v) os << vv << ","; return os << "}"; } template std::ostream& operator<<(std::ostream& os, std::pair p) { return os << "(" << p.first << "," << p.second << ")"; } template std::ostream& operator<<(std::ostream& os, std::map v) { os << "{"; for (auto vv : v) os << vv << ","; return os << "}"; } template std::ostream& operator<<(std::ostream& os, std::queue q) { os << "["; while (!q.empty()) { os << q.front() << ","; q.pop(); } return os << "]"; } template std::ostream& operator<<(std::ostream& os, std::priority_queue q) { os << "{"; while (!q.empty()) { os << q.top() << ","; q.pop(); } return os << "}"; } /* ----- Short Functions ----- */ template T Vec(T v) { return v; } template auto Vec(size_t l, Ts... ts) { return std::vector(ts...))>(l, Vec(ts...)); } template inline T sq(T a) { return a * a; } template inline T iceil(T n, T d) { return (n + d - 1) / d; } template T gcd(T a, T b) { while (b > 0) { a %= b; swap(a, b); } return a; } template T ipow(T b, U n) { T ret = 1; while (n > 0) { if (n & 1) ret *= b; n >>= 1; b *= b; } return ret; } // 0-indexed template inline T kthbit(T a, U k) { return (a >> k) & 1; } template inline T mask(T a, U k) { return a & ((1 << k) - 1); } /* ----- class ----- */ template struct Edge { int from, to; T cost; Edge(int from = -1, int to = -1, T cost = 1) : from(from), to(to), cost(cost){}; bool operator<(const Edge& e) const { return this->cost < e.cost; } bool operator>(const Edge& e) const { return this->cost > e.cost; } }; template class Graph { public: explicit Graph(int N = 0) : size(N) { path.resize(size); } void span(int u, int v, T cost = 1) { path[u].push_back(Edge(u, v, cost)); } std::vector> operator[](int v) const { return path[v]; } int size; std::vector>> path; }; /* ----- Constants ----- */ // const int INF = 1 << 25; // const ll INF = 1LL << 50; // const ld PI = acos(-1); // const ld EPS = 1e-10; // mt19937 mt(ll(time(0))); const ll INF = std::numeric_limits::max(); template std::pair, std::vector> dijkstra(const Graph& graph, int s) { std::vector dist(graph.size, INF), pdist(graph.size, INF); dist[s] = pdist[s] = 0; std::priority_queue, std::vector>, std::greater>> que; que.emplace(0, s); while (!que.empty()) { int v; T d; std::tie(d, v) = que.top(); que.pop(); if (d > dist[v]) continue; for (const auto& e : graph[v]) { pdist[e.to] = std::min(pdist[e.to], dist[e.from]); pdist[e.to] = std::min(pdist[e.to], pdist[e.from] + e.cost); if (dist[e.to] <= dist[v] + e.cost) continue; dist[e.to] = dist[v] + e.cost; que.emplace(dist[e.to], e.to); } } return std::make_pair(dist, pdist); } int main() { int N, M; std::cin >> N >> M; Graph graph(N); for (int i = 0; i < M; ++i) { int u, v; ll c; std::cin >> u >> v >> c; --u, --v; graph.span(u, v, c); graph.span(v, u, c); } std::vector dist, pdist; std::tie(dist, pdist) = dijkstra(graph, 0); for (int v = 0; v < N; ++v) { std::cout << dist[v] + pdist[v] << std::endl; } return 0; }