#include <fstream>
#include <queue>
#pragma GCC optimize("O3", "Ofast", "unroll-loops")
#pragma GCC target("avx2")
const int MAXN = 50'000;
const int MAXM = 250'000;
const int BUFSIZE = (65'536);
const int INF = 2'000'000'000;
int head[MAXM + 1];
const int NIL = -1;
int n, m;
struct Edge{
int u, cost;
bool operator <(const Edge &a) const {
return cost > a.cost;
}
};
int nl = 0;
struct List{
int nxt;
Edge e;
}list[MAXM + 1];
void addEdge(int u, int v, int w) {
list[nl] = {head[u], {v, w}};
head[u] = nl++;
}
class InParser {
private:
FILE * fin;
char * buff;
int sp;
char read_ch() {
if(!(sp = (sp + 1) & (BUFSIZE - 1))) {
fread(buff, 1, BUFSIZE, fin);
}
return buff[sp];
}
public:
InParser(const char * nume) {
fin = fopen(nume, "r");
buff = new char[BUFSIZE]();
sp = 4050;
}
InParser& operator >> (int &n) {
char c;
while(!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if(c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while(isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while(!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if(c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while(isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
class OutParser {
private:
FILE *fout;
char *buff;
int sp;
void write_ch(char ch) {
if (sp == 50010) {
fwrite(buff, 1, 50010, fout);
sp = 0;
buff[sp++] = ch;
} else {
buff[sp++] = ch;
}
}
public:
OutParser(const char* name) {
fout = fopen(name, "w");
buff = new char[50010]();
sp = 0;
}
~OutParser() {
fwrite(buff, 1, sp, fout);
fclose(fout);
}
OutParser& operator << (int vu32) {
if (vu32 <= 9) {
write_ch(vu32 + '0');
} else {
(*this) << (vu32 / 10);
write_ch(vu32 % 10 + '0');
}
return *this;
}
OutParser& operator << (long long vu64) {
if (vu64 <= 9) {
write_ch(vu64 + '0');
} else {
(*this) << (vu64 / 10);
write_ch(vu64 % 10 + '0');
}
return *this;
}
OutParser& operator << (char ch) {
write_ch(ch);
return *this;
}
OutParser& operator << (const char *ch) {
while (*ch) {
write_ch(*ch);
++ch;
}
return *this;
}
};
InParser fin("dijkstra.in");
OutParser fout("dijkstra.out");
int dist[MAXN + 1];
void dijkstra(int node) {
int i, newdist, newnode, y;
Edge e;
std::priority_queue<Edge> pq;
pq.push({node, 0});
memset(dist, INF, sizeof(dist));
dist[node] = 0;
while(!pq.empty()) {
e = pq.top();
pq.pop();
if(e.cost == dist[e.u]) {
for(y = head[e.u]; y != NIL; y = list[y].nxt) {
Edge nxt = list[y].e;
newdist = nxt.cost + dist[e.u];
newnode = nxt.u;
if(newdist < dist[nxt.u]) {
pq.push({newnode, newdist});
dist[nxt.u] = newdist;
}
}
}
}
for( int i = 2; i <= n; i++ ) {
if(dist[i] == INF)
dist[i] = 0;
fout << dist[i] << " ";
}
}
int main(){
int i, u, v, w;
fin >> n >> m;
for( i = 1; i <= m; i++ ) {
head[i] = NIL;
}
for( i = 0; i < m; i++ ) {
fin >> u >> v >> w;
addEdge(u, v, w);
}
dijkstra(1);
return 0;
}