#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
ios::sync_with_stdio(false);cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<pair<int, int>>>routes(n+1);
for(int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
routes[a].push_back({c, b});
}
set<pair<int, int>>queu;
vector<bool>visited(n+1, 0);
vector<int>dist(n+1, 1e18);
queu.insert({0, 1});
while(!queu.empty()) {
int root = (*queu.begin()).second;
int curdist = (*queu.begin()).first;
queu.erase(queu.begin());
if(visited[root]) continue;
visited[root] = true;
dist[root] = min(dist[root], curdist);
for(int i = 0; i < routes[root].size(); i++) {
if(visited[routes[root][i].second]) continue;
queu.insert({curdist+routes[root][i].first, routes[root][i].second});
}
}
for(int i = 1; i <= n; i++) {
cout << dist[i] << " ";
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBpbnQgbG9uZyBsb25nIGludAoKaW50MzJfdCBtYWluKCkgewogIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTtjaW4udGllKG51bGxwdHIpOwogIGludCBuLCBtOwogIGNpbiA+PiBuID4+IG07CiAgdmVjdG9yPHZlY3RvcjxwYWlyPGludCwgaW50Pj4+cm91dGVzKG4rMSk7CiAgZm9yKGludCBpID0gMDsgaSA8IG07IGkrKykgewogICAgaW50IGEsIGIsIGM7CiAgICBjaW4gPj4gYSA+PiBiID4+IGM7CiAgICByb3V0ZXNbYV0ucHVzaF9iYWNrKHtjLCBifSk7CiAgfQogIHNldDxwYWlyPGludCwgaW50Pj5xdWV1OwogIHZlY3Rvcjxib29sPnZpc2l0ZWQobisxLCAwKTsKICB2ZWN0b3I8aW50PmRpc3QobisxLCAxZTE4KTsKICBxdWV1Lmluc2VydCh7MCwgMX0pOwogIHdoaWxlKCFxdWV1LmVtcHR5KCkpIHsKICAgIGludCByb290ID0gKCpxdWV1LmJlZ2luKCkpLnNl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