#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 = 100000; const int dyx[4][2] = { { 0, 1}, {-1, 0}, {0,-1}, {1, 0} }; map d[2]; vector< vector > edges(MAX_N+1); vector longest_edge(MAX_N+1, 0); void dijkstra(ll start, int shortcut){ priority_queue< Pll, vector, greater > que; d[shortcut][start] = 0; que.push(Pll(0, start)); while(!que.empty()){ Pll v = que.top(); que.pop(); // if(d[shortcut].find(v.second) != d[shortcut].end() && d[shortcut][v.second] < v.first) continue; for(Pll e: edges[v.second]){ if(shortcut) { if(d[1].find(e.first) == d[1].end() || d[1][e.first] > d[1][v.second] + longest_edge[v.second] + e.second - max(longest_edge[v.second], e.second)){ d[1][e.first] = d[1][v.second] + longest_edge[v.second] + e.second - max(longest_edge[v.second], e.second); que.push(Pll(d[1][e.first], e.first)); longest_edge[e.first] = max(longest_edge[v.second], e.second); } } else { if(d[0].find(e.first) == d[0].end() || d[0][e.first] > d[0][v.second] + e.second){ d[0][e.first] = d[0][v.second] + e.second; que.push(Pll(d[0][e.first], e.first)); } } } } } 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; } }