#include #include #include #include #include using ll = long long; 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; }; const ll INF = std::numeric_limits::max(); template std::vector dijkstra(const Graph& graph, std::vector ss) { std::vector dist(graph.size, INF); std::priority_queue, std::vector>, std::greater>> que; for (auto s : ss) { dist[s] = 0; 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]) { if (dist[e.to] <= dist[v] + e.cost) continue; dist[e.to] = dist[v] + e.cost; que.emplace(dist[e.to], e.to); } } return dist; } int main() { int N, M; std::cin >> N >> M; Graph graph(N * 2); 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); graph.span(u, v + N, 0); graph.span(v, u + N, 0); graph.span(u + N, v + N, c); graph.span(v + N, u + N, c); } auto dist = dijkstra(graph, {0, N}); for (int v = 0; v < N; ++v) { std::cout << dist[v] + dist[v + N] << std::endl; } return 0; }