#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define debug(x) cerr << #x << ": " << x << endl using namespace std; typedef long long ll; typedef pair Pll; typedef pair Pii; const ll MOD = 1000000007; const long double EPS = 1e-10; const int MAX_N = 112345; const int dyx[4][2] = { { 0, 1}, {-1, 0}, {0,-1}, {1, 0} }; map d[2]; vector< vector > edges(MAX_N); struct State { ll d, longest_edge; int node; State(int node, ll d, ll longest_edge): node(node), d(d), longest_edge(longest_edge) {} bool operator>(const State& s) const{ return d+longest_edge > s.d+s.longest_edge; } }; void dijkstra(ll start, int shortcut){ priority_queue, greater > que; d[shortcut][start] = 0; que.push(State(start, 0, 0)); while(!que.empty()){ State v = que.top(); que.pop(); if(d[shortcut].find(v.node) != d[shortcut].end() && d[shortcut][v.node] < v.d) continue; for(Pll e: edges[v.node]){ if(shortcut) { if(d[1].find(e.first) == d[1].end() || d[1][e.first] > d[1][v.node] + v.longest_edge + e.second - max(v.longest_edge, e.second)){ d[1][e.first] = d[1][v.node] + v.longest_edge + e.second - max(v.longest_edge, e.second); que.push(State(e.first, d[1][e.first], max(v.longest_edge, e.second))); } } else { if(d[0].find(e.first) == d[0].end() || d[0][e.first] > d[0][v.node] + e.second){ d[0][e.first] = d[0][v.node] + e.second; que.push(State(e.first, d[0][e.first], 0)); } } } } } int main() { std::ios::sync_with_stdio(0); cin.tie(0); int n,m,a,b; ll c; cin >> n >> m; for(int i=0;i> a >> b >> c; edges[a].emplace_back(b, c); edges[b].emplace_back(a, c); } dijkstra(1LL, 0); dijkstra(1LL, 1); for(int i=1;i<=n;++i) { cout << d[0][i] + d[1][i] << endl; // cerr << i << ": " << d[0][i] << " " << d[1][i] << " " << longest_edge[i] << endl; } }