#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using int64 = long long; template struct Range { struct RangeIterator { T i, step; RangeIterator(T i, T step): i{i}, step{step} {} T& operator*() { return i; } RangeIterator& operator++() { i += step; return *this; } bool operator!=(const RangeIterator& rhs) const { return (step > 0 && i < rhs.i) || (step < 0 && i > rhs.i); } }; T start, stop, step; Range(T start, T stop, T step): start{start}, stop{stop}, step{step} {} Range(T start, T stop): Range(start, stop, 1) {} Range(T stop): Range(0, stop, 1) {} RangeIterator begin() const { return RangeIterator(start, step); } RangeIterator end() const { return RangeIterator(stop, step); } }; struct Edge { using Weight = int64; int end1_, end2_, from_, to_; Weight weight_; Edge() = default; Edge(int end1, int end2, Weight w): end1_{end1}, end2_{end2}, from_{end1}, to_{end2}, weight_{w} {} bool operator<(const Edge& e) const { return weight_ < e.weight_; } }; struct UndirectedGraph { int num_nodes_; std::vector> adjacent_list_; std::vector edges_; UndirectedGraph() = default; UndirectedGraph(int num_node): num_nodes_{num_node}, adjacent_list_(num_node) {} const std::vector& operator[](int u) const { return adjacent_list_[u]; } std::vector& operator[](int u) { return adjacent_list_[u]; } void addEdge(int end1, int end2, Edge::Weight weight) { adjacent_list_[end1].emplace_back(end1, end2, weight); adjacent_list_[end2].emplace_back(end2, end1, weight); edges_.emplace_back(end1, end2, weight); } }; int64 cost[100000][2]; void dijkstra(const UndirectedGraph& graph, int start) { struct State { int node; int use_ticket; int64 cost; State(int node, int use_ticket, int64 cost): node{node}, use_ticket{use_ticket}, cost{cost} {} bool operator>(const State& rhs) const { return cost > rhs.cost; } }; constexpr int64 INF = (1LL << 50); for (int u = 0; u < graph.num_nodes_; u++) { cost[u][0] = cost[u][1] = INF; } std::priority_queue, std::greater> pque; cost[start][0] = cost[start][1] = 0; pque.emplace(start, 0, 0); while (!pque.empty()) { State st = pque.top(); pque.pop(); if (cost[st.node][st.use_ticket] < st.cost) continue; for (const Edge& e : graph[st.node]) { for (int use_ticket : {0, 1}) { if (st.use_ticket && use_ticket) continue; int64 ncost = st.cost + (use_ticket ? 0 : e.weight_); if (cost[e.to_][st.use_ticket | use_ticket] > ncost) { cost[e.to_][st.use_ticket | use_ticket] = ncost; pque.emplace(e.to_, st.use_ticket | use_ticket, ncost); } } } } } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n, m; std::cin >> n >> m; UndirectedGraph graph(n); for (const int i : Range<>(m)) { int a, b, c; std::cin >> a >> b >> c; a--; b--; graph.addEdge(a, b, c); } dijkstra(graph, 0); for (const int i : Range<>(n)) { std::cout << cost[i][0] + cost[i][1] << std::endl; } return 0; }