#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define FOR(i,a,b) for(ll i=(a);i<(b);++i) #define ALL(v) (v).begin(), (v).end() #define p(s) cout<<(s)<(const Node &another) const { return distance > another.distance; } }; struct DijkstraBase{ vector d; // 距離 vector > graph; vector done; // ノード数を入れる void initialize(ll size){ d.resize(size); done.resize(size); graph.resize(size); FOR(i, 0, size){ d[i] = inf; done[i] = false; } } ll distance(int i){ if(d.size()<=i) return -1; return d[i]; } void print_graph(){ FOR(i, 0, graph.size()){ cout << i << " -> "; for(auto edge : graph[i]){ cout << edge.to << " "; } cout << endl; } } void register_edge(ll a, ll b, ll cost){ auto edge = Edge(b, cost); graph[a].push_back(edge); } void calc_shortest_path(){ priority_queue, greater > que; auto node = Node(0, 0); que.push(node); while(!que.empty()){ // 1番distanceが小さいノードを取り出す Node n = que.top(); que.pop(); if(done[n.index]) continue; done[n.index] = true; d[n.index] = n.distance; auto edge_list = graph[n.index]; for(auto edge : edge_list){ // 短くなるノードがあれば if(!done[edge.to] && n.distance + edge.cost < d[edge.to]){ ll shorter_distance = n.distance + edge.cost; auto node = Node(shorter_distance, edge.to); que.push(node); } } } } }; struct Dijkstra : public DijkstraBase{ }; Dijkstra dij; struct DijkstraTicket : public DijkstraBase{ void calc_shortest_path(){ ll N = graph.size(); FOR(i, 0, N){ d[i] = dij.distance(i); } priority_queue, greater > que; auto node = Node(0, 0); que.push(node); while(!que.empty()){ Node n = que.top(); que.pop(); if(done[n.index]) continue; done[n.index] = true; d[n.index] = n.distance; auto edge_list = graph[n.index]; for(auto edge : edge_list){ ll dist0 = dij.distance(n.index) + 0; // 最後にチケットを使った ll dist1 = d[n.index] + edge.cost; // チケットを使ってiまで来て、最後の一歩は普通に if(!done[edge.to]){ auto node0 = Node(dist0, edge.to); auto node1 = Node(dist1, edge.to); que.push(node0); que.push(node1); } // 短くなるノードがあれば // if(!done[edge.to] && n.distance + edge.cost < d[edge.to]){ // ll shorter_distance = n.distance + edge.cost; // auto node = Node(shorter_distance, edge.to); // que.push(node); // } } } // FOR(i, 0, N){ // // iまで普通に行って、そこでチケットを使う場合 // ll dist = dij.distance(i) + 0; // auto edge_list = graph[i]; // for(auto edge : edge_list){ // d[edge.to] = min(d[edge.to], dist); // } // } } }; template < typename T > void vprint(T &V){ for(auto v : V){ cout << v << " "; } cout << endl; } int main(){ cin.tie(0); ios::sync_with_stdio(false); // input ll N, M; cin >> N >> M; dij.initialize(N); DijkstraTicket dijT; dijT.initialize(N); FOR(i, 0, M){ ll a, b, c; cin >> a >> b >> c; a--; b--; dij.register_edge(a, b, c); dij.register_edge(b, a, c); dijT.register_edge(a, b, c); dijT.register_edge(b, a, c); } dij.calc_shortest_path(); // dij.print_graph(); // p(dij.distance(4)); dijT.calc_shortest_path(); // p(dijT.distance(4)); // br(); // p("answer"); FOR(i, 0, N){ ll ans = dij.distance(i) + dijT.distance(i); p(ans); } return 0; }