#include <cstdio>
#include <vector>
#include <queue>
#include <bitset>
#define MAXN 50005
#define oo ((1LL<<31)-1)
#define FIN "dijkstra.in"
#define FOUT "dijkstra.out"
using namespace std;
vector<int> V[ MAXN ];
vector<int> C[ MAXN ];
int distMin[ MAXN ];
queue<int> Queue;
bitset<MAXN> inQueue(0);
int nodes,
edges;
//prototpes functions
void readData();
void Dijkstra();
void writeData();
void printV();
//main function
int main() {
readData();
Dijkstra();
writeData();
return(0);
};
void readData() {
int x,
y,
cost;
//freopen(FIN, "r", stdin);
scanf("%d %d", &nodes, &edges);
for(int ed = 1; ed <= edges; ed++) {
scanf("%d %d %d", &x, &y, &cost);
V[ x ].push_back( y );
C[ x ].push_back( cost );
}
fclose( stdin );
};
void Dijkstra() {
for(int i = 2; i <= nodes; i++) distMin[ i ] = oo;
distMin[ 1 ] = 0;
Queue.push( 1 );
inQueue[ 1 ] = true;
while(!Queue.empty()) {
int curr = Queue.front();
Queue.pop();
inQueue[ curr ] = false;
for(int i = 0; i < V[ curr ].size(); i++) {
int y = V[ curr ][ i ];
int cost = C[ curr ][ i ];
if(distMin[ y ] > distMin[ curr ] + cost) {
distMin[ y ] = distMin[ curr ] + cost;
if(!inQueue[ y ]) {
Queue.push( y );
inQueue[ y ] = true;
}
}
}
}
};
void writeData() {
//freopen(FOUT, "w", stdout);
for(int i = 2; i <= nodes; i++) printf("%d ", (distMin[ i ] < oo) ? distMin[ i ] : 0);
fclose( stdout );
};
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8Yml0c2V0PgojZGVmaW5lIE1BWE4gNTAwMDUKI2RlZmluZSBvbyAoKDFMTDw8MzEpLTEpCiNkZWZpbmUgRklOICJkaWprc3RyYS5pbiIKI2RlZmluZSBGT1VUICJkaWprc3RyYS5vdXQiCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdmVjdG9yPGludD4gVlsgTUFYTiBdO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nJvbnQoKTsKCiAgICAgICAgICAgIFF1ZXVlLnBvcCgpOwoKICAgICAgICAgICAgaW5RdWV1ZVsgY3VyciBdID0gZmFsc2U7ICAgIAoKICAgICAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IFZbIGN1cnIgXS5zaXplKCk7IGkrKykgewoKICAgICAgICAgICAgICAgICAgICBpbnQgeSA9IFZbIGN1cnIgXVsgaSBdOwoKICAgICAgICAgICAgICAgICAgICBpbnQgY29zdCA9IENbIGN1cnIgXVsgaSBdOwoKICAgICAgICAgICAgICAgICAgICBpZihkaXN0TWluWyB5IF0gPiBkaXN0TWluWyBjdXJyIF0gKyBjb3N0KSB7CgogICAgICAgICAgICAgICAgICAgICAgIGRpc3RNaW5bIHkgXSA9IGRpc3RNaW5bIGN1cnIgXSArIGNvc3Q7CgogICAgICAgICAgICAgICAgICAgICAgIGlmKCFpblF1ZXVlWyB5IF0pIHsKCiAgICAgICAgICAgICAgICAgICAgICAgICAgIFF1ZXVlLnB1c2goIHkgKTsKCiAgICAgICAgICAgICAgICAgICAgICAgICAgIGluUXVldWVbIHkgXSA9IHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gCiAgICAgfQp9OwoKdm9pZCB3cml0ZURhdGEoKSB7CgogICAgIC8vZnJlb3BlbihGT1VULCAidyIsIHN0ZG91dCk7CgogICAgIGZvcihpbnQgaSA9IDI7IGkgPD0gbm9kZXM7IGkrKykgcHJpbnRmKCIlZCAiLCAoZGlzdE1pblsgaSBdIDwgb28pID8gZGlzdE1pblsgaSBdIDogMCk7CgogICAgIGZjbG9zZSggc3Rkb3V0ICk7Cn07