#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
const int inf=9999999;
using namespace std;
int main()
{
int n,m,i,j,k;
int u[10],v[10],w[10];
int first[10],next[10],book[10],dis[10];
scanf("%d%d",&n,&m);
memset(book,0,sizeof(book));
memset(first,-1,sizeof(first));
for(i=1;i<=n;i++)
dis[i]=inf;
dis[1]=0;
for(i=1; i<=m; i++) //单向邻接表
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
next[i]=first[u[i]];
first[u[i]]=i;
}
queue<int>q;
int head=1;
q.push(head);
book[head]=1;
while(!q.empty())
{
//book[q.front()]=0;//这个标记可以放在while循环之后,k赋值之前之后,但是不可放在pop之后
k=first[q.front()];
q.pop();
while(k!=-1)
{
if(dis[v[k]]>dis[u[k]]+w[k])
{
dis[v[k]]=dis[u[k]]+w[k];
if(book[v[k]]==0)
{
q.push(v[k]);
book[v[k]]=1;
}
}
k=next[k];
}
// book[q.front()]=0;
}
for(i=1;i<=n;i++)
printf("%d ",dis[i]);
return 0;
}
/*
5 7
1 2 2
1 5 10
2 3 3
2 5 7
3 4 4
4 5 5
5 3 6
*/
#include<string>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
const int inf=9999999;
using namespace std;
int main()
{
int n,m,i,j,k;
int u[10],v[10],w[10];
int first[10],next[10],book[10],dis[10];
scanf("%d%d",&n,&m);
memset(book,0,sizeof(book));
memset(first,-1,sizeof(first));
for(i=1;i<=n;i++)
dis[i]=inf;
dis[1]=0;
for(i=1; i<=m; i++) //单向邻接表
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
next[i]=first[u[i]];
first[u[i]]=i;
}
queue<int>q;
int head=1;
q.push(head);
book[head]=1;
while(!q.empty())
{
//book[q.front()]=0;//这个标记可以放在while循环之后,k赋值之前之后,但是不可放在pop之后
k=first[q.front()];
q.pop();
while(k!=-1)
{
if(dis[v[k]]>dis[u[k]]+w[k])
{
dis[v[k]]=dis[u[k]]+w[k];
if(book[v[k]]==0)
{
q.push(v[k]);
book[v[k]]=1;
}
}
k=next[k];
}
// book[q.front()]=0;
}
for(i=1;i<=n;i++)
printf("%d ",dis[i]);
return 0;
}
/*
5 7
1 2 2
1 5 10
2 3 3
2 5 7
3 4 4
4 5 5
5 3 6
*/