#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50000;
const int INF = 1e9;
vector<pair<int, int>> L[NMAX + 1];
int d[NMAX + 1];
bool vis[NMAX + 1];
int main() {
int n, m;
fin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, c;
fin >> x >> y >> c;
L[x].push_back({y, c});
}
for (int i = 1; i <= n; i++) {
d[i] = INF;
}
d[1] = 0;
priority_queue<pair<int, int>> pq;
pq.push({-d[1], 1});
while (!pq.empty()) {
int nod = pq.top().second;
pq.pop();
if (vis[nod] == 1) continue;
vis[nod] = 1;
for (auto it : L[nod]) {
int vec = it.first;
int cost = it.second;
if (d[vec] > d[nod] + cost) {
d[vec] = d[nod] + cost;
pq.push({-d[vec], vec});
}
}
}
for (int i = 2; i <= n; i++) {
if (d[i] == INF) fout << 0 << " ";
else fout << d[i] << " ";
}
fout << "\n";
return 0;
}