#include <bits/stdc++.h>
#define NMAX 50005
using namespace std;
const int INF = 0x3f3f3f3f;
ifstream fin("dijkstra.in") ;
ofstream fout("dijkstra.out") ;
int n, m, x, y, node, cost;
int dist[NMAX], valid[NMAX] ;
struct Acompare
{
bool operator()(pair <int, int> &p1, pair<int , int> &p2)
{
return p1.second > p2.second ;
}
};
priority_queue < pair <int,int>, vector <pair<int , int>>, Acompare> Q ;
vector < pair <int,int> > v[NMAX] ;
int main()
{
fin >> n >> m ;
for(int i=1; i<=m; i++)
{
fin >> x >> y >> cost ;
v[x].push_back({y, cost}) ;
//v[y].push_back({x, cost}) ;
}
for(int i=1; i<=n; i++)
dist[i] = INF ;
dist[1] = 0 ;
Q.push({1, 0}) ;
while(!Q.empty())
{
node = Q.top().first ;
cost = Q.top().second ;
Q.pop() ;
if(dist[node] != cost)
continue ;
for(auto it : v[node])
{
if(dist[it.first] > cost + it.second)
{
dist[it.first] = cost + it.second ;
Q.push({it.first, dist[it.first]}) ;
}
}
}
for(int i=2; i<=n; i++)
{
if(dist[i] == INF)
dist[i] = 0;
fout << dist[i] << " " ;
}
return 0;
}