#include<iostream> #include<algorithm> #include<cstring> #include<queue> #include<vector> using namespace std ; const long long N = 3e5 + 2 , INF = 0x3f3f3f3f3f3f3f3fLL; int n , m ; struct edge{ int from , to ; long long w ; edge(int a ,int b , long long c){ from = a , to = b , w = c ; } }; vector<edge> e[N] ; struct s_node{ int id ; long long n_dis ; s_node(int b , long long c ){ id = b ; n_dis = c ; } bool operator < (const s_node &a ) const { return n_dis > a.n_dis ; } }; int pre[N] ; void print_path(int s , int t){ if(s == t) { printf("%d",s) ; return ; } print_path(s,pre[t]) ; printf("%d",t) ; } long long dis[N] ; void dijkstra(){ int s = 1 ; bool done[N] ; for(int i = 1 ; i <= n ; i++) { dis[i] = INF ; done[i] = false ; } dis[s] = 0 ; priority_queue<s_node> q ; q.push(s_node(s,dis[s])) ; while(!q.empty()){ s_node u = q.top() ; q.pop() ; if(done[u.id]) continue ; done[u.id] = true ; for(int i = 0 ; i < e[u.id].size() ; i ++){ edge y = e[u.id][i] ; if(done[y.to]) continue ; if(dis[y.to] > y.w + u.n_dis){ dis[y.to] = y.w + u.n_dis ; q.push(s_node(y.to , dis[y.to])) ; pre[y.to] = u.id ; } } } } int main(){ cin >> n >> m ; for(int i = 1 ; i <= m ; i ++){ int a, b ,c ; cin >> a >> b >> c ; e[a].push_back(edge(a,b,c)) ; } dijkstra() ; for(int i = 1; i <=n ;i ++){ if(dis[i] >= INF) cout << "-1 " ; else printf("%lld ",dis[i]) ; } }