/*#include<bits/stdc++.h>
#define Inf 10000
using namespace std;
int n,circuit=0,viz[100]={0},a[20][20],a2[20][20];
void citire(int a[][100],int &n)
{ int m,i,j;
ifstream fin("dijkstra.in");
fin>>n>>m;
for( i=1;i<=n;i++)
for( j=1;j<=n;j++)
if(i==j) a[i][j]=0;
else a[i][j]=Inf;
for(i=1;i<=m;i++)
{int x,y,z;
fin>>x>>y>>z;
a[x][y]=z;
}
}
void afisare(int a[][100],int n)
{
int i,j;
for(i=1;i<=n;i++)
{for(j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
void royfloyd(int a[][100],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
{for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]>a[i][k]+a[k][j]) a[i][j]=a[i][k]+a[k][j];
//pred[k]=i,pred[j]=k;
// afisare(a,n);
// cout<<endl;
}
}
void dijkstra(int a[][100],int n,int x,int pred[],int d[])
{
int i,j,k,viz[100]={0},aa,y;
for(i=1;i<=n;i++)
{
d[i]=a[x][i];
pred[i]=x;
}
pred[x]=0;
viz[x]=1;
for(i=1;i<=n-1;i++)
{
aa=Inf;
for(j=1;j<=n;j++) if(d[j]<aa && !viz[j])
{
aa=d[j];
y=j;
}
if(aa!=Inf)
{
viz[y]=1;
for(j=1;j<=n;j++)
if(!viz[j] && d[j]>d[y]+a[y][j])
{ d[j]=d[y]+a[y][j];
pred[j]=y;
}
}
}
}
void Afisare(int pred[100],int y)
{
int i,j,k;
while(y)
{
cout<<y;
y=pred[y];
}
}
void Afisare_r(int pred[],int y)
{
if(y) {Afisare_r(pred,pred[y]);
cout<<y;}
}
int main()
{ int a[100][100],i,j,n,pred[100],d[100],y;
citire(a,n);
//royfloyd(a,n);
// afisare(a);
//cout<<sizeof(long long);
//t:sa se afiseze costul minim pentru drumul dintre 2 noduri x si y si drumul .
dijkstra(a,n,1,pred,d);
ofstream g("dijkstra.out");
//for(i=2;i<=n;i++) g<<d[i]<<' ';
//for(i=1;i<=n;i++) cout<<d[i]<<" ";
//cin>>y;
//Afisare(pred,y);
//cin>>y;
//Afisare_r(pred,y);
//t:sa se afiseze drumul minim dintre nodul x si oricare alt nod din graf, folosind dijkstra
//t:bile si auto
}*/
//cu liste de vecini
#include <bits/stdc++.h>
#define Dim 50001
#define Jaime 1<<30
using namespace std;
struct nod{int inf,cost;nod *adr;};
nod *v[Dim];
int n,d[Dim],m,i,q[Dim];
void add(nod *&p,int x,int y)
{
nod *nou,*q;
nou=new nod;
nou->inf=x;
nou->cost=y;
nou->adr=NULL;
if(p==NULL) p=nou;
else{ q=p;
while(q->adr) q=q->adr;
q->adr=nou;
}
}
void citire()
{ int m,i,x,y,z;
ifstream fin("poveste.in");
fin>>n>>m;
for(i=1;i<=n;i++) v[i]=NULL;
for(i=1;i<=m;i++)
{ fin>>x>>y>>z;
add(v[x],y,z);
}
}
void add_1(int where, int what, int cost)
{
nod *q = new nod;
q->inf = what;
q->cost = cost;
q->adr = v[where];
v[where] = q;
}
void read()
{ifstream f("dijkstra.in");
f>>n>>m;
int x, y, z;
for ( int i = 1; i <= m; ++i )
{
f>>x>>y>>z;
add_1(x, y, z);
}
}
void dijkstra(int x)
{
int i,j,k,viz[Dim]={0},aa,y=0;
for(i=2;i<=n;i++) d[i]=Jaime;
for(i=1;i<=n;i++)
{
aa=Jaime;
for(j=1;j<=n;j++) if(d[j]<aa && !viz[j])
{
aa=d[j];
y=j;
}
if(aa!=Jaime)
{
viz[y]=1;
nod *t=v[y];
while(t)
{
if ( d[t->inf] > d[y] + t->cost ) d[t->inf] = d[y] + t->cost;
t=t->adr;
}
}
}
}
void dijkstra_1()
{
for ( int i = 2; i <= n; ++i )
d[i] = Jaime;
int min, pmin = 0;
for ( int i = 1; i <= n; ++i )
{
min = Jaime;
for ( int j = 1; j <= n; ++j )
if ( d[j] < min && !q[j] )
min = d[j], pmin = j;
q[pmin] = 1;
nod *t = v[pmin];
while ( t )
{
if ( d[ t->inf ] > d[pmin] + t->cost )
d[ t->inf ] = d[pmin] + t->cost;
t = t->adr;
}
}
}
void print(nod *p)
{
while(p)
{
cout<<p->inf<<' '<<p->cost;
p=p->adr;
}
cout<<endl;
}
int main()
{
read();
//print(v[1]);
dijkstra(1);
//dijkstra_1();
ofstream g("dijkstra.out");
for(i=2;i<=n;i++) if(d[i]==Jaime ) g<<'0'<<' ';
else g<<d[i]<<' ';
g<<'\n';
return 0;
}
/*#include <bits/stdc++.h>
#define maxn 50001
#define jaime 1 << 30
using namespace std;
FILE *in = fopen("dijkstra.in","r"), *out = fopen("dijkstra.out","w");
struct nod
{
int inf, cost;
nod *adr;
};
int n, m;
nod *v[maxn];
int d[maxn], q[maxn];
void add(nod *&p,int x,int y)
{
nod *nou,*q;
nou=new nod;
nou->inf=x;
nou->cost=y;
nou->adr=NULL;
if(p==NULL) p=nou;
else{ q=p;
while(q->adr) q=q->adr;
q->adr=nou;
}
}
void citire()
{ int m,i,x,y,z;
fscanf(in, "%d %d", &n, &m);
for(i=1;i<=n;i++) v[i]=NULL;
for(i=1;i<=m;i++)
{ fscanf(in, "%d %d %d", &x, &y, &z);
add(v[x],y,z);
}
}
void dijkstra()
{
for ( int i = 2; i <= n; ++i )
d[i] = jaime;
int min, pmin = 0;
for ( int i = 1; i <= n; ++i )
{
min = jaime;
for ( int j = 1; j <= n; ++j )
if ( d[j] < min && !q[j] )
min = d[j], pmin = j;
q[pmin] = 1;
nod *t = v[pmin];
while ( t )
{
if ( d[ t->inf ] > d[pmin] + t->cost )
d[ t->inf ] = d[pmin] + t->cost;
t = t->adr;
}
}
}
int main()
{
citire();
dijkstra();
for ( int i = 2; i <= n; ++i )
fprintf(out, "%d ", d[i] == jaime ? 0 : d[i]);
fprintf(out, "\n");
return 0;
}*/