#!/usr/bin/python # -*- coding: utf-8 -*- # import from sys import stdin, setrecursionlimit setrecursionlimit(10**8) # def def int_map(): return map(int,input().split()) def int_list(): return ['Null']+list(map(int,input().split())) # 整数のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.) def int_mtx(N): x = [['Null']+list(map(int, stdin.readline().split())) for _ in range(N)] x.insert(0,['Null']) return x # 文字のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.) # AAAAA # BBBBB # CCCCC # のような標準入力を # '0AAAAA' # '0BBBBB' # '0CCCCC' # と受け取る def str_mtx(N): x = ['0'+stdin.readline()[:-1] for _ in range(N)] x.insert(0,'0') return x # 文字のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.) # AAAAA XXXXX # BBBBB YYYYY # CCCCC ZZZZZ # のような標準入力を # [[['0'],[AAAAA],[XXXXX]], # [['0'],[BBBBB],[YYYYY]], # [['0'],[CCCCC],[ZZZZZ]] ] # と受け取る def str_list(N): x = [['0']+list(map(str, stdin.readline().split())) for _ in range(N)] x.insert(0,['0']) return x # 高さH+1, 幅W+1, のゼロ行列の作成 def zero_mtx(H,W): x = [[0]*(W+1) for i in range(H+1)] return x # リストをスペースで分割する(先頭を省く) def print_space(l): return print(" ".join([str(x) for x in l[1:]])) # ゼロインデックスの場合 # 整数のリストのリスト:N行のリスト(各行のリストは同じ長さでなくてよい.) def int_mtx_0(N): x = [list(map(int, stdin.readline().split())) for _ in range(N)] return x # ゼロインデックスの場合 # 文字のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.) def str_mtx_0(N): x = [list(stdin.readline()[:-1]) for _ in range(N)] return x # ゼロインデックスの場合 # リストをスペースで分割する(先頭を省かない) def print_space_0(l): return print(" ".join([str(x) for x in l])) # ゼロインデックスの場合 # 高さH, 幅W, のゼロ行列の作成 def zero_mtx_0(H,W): x = [[0]*W for i in range(H)] return x def int_list_0(): return list(map(int,input().split())) ## インポート # from collections import deque # 順列に使う # import itertools # 最大公約数などに使う # import math # リストの要素の数をdict形式で # import collections # 二次元配列のコピーを作りたいとき # a_copy = deepcopy(a) # from copy import deepcopy # main code N,M = int_map() ABCX = int_mtx_0(M) """ ダイクストラ法(ヒープによる優先度付きキューを用いて) 計算量 O( |E|*log(|V|)) E:辺の数 V:頂点数 ベルマンフォードより早いが,負のコストがあると使えない. """ import heapq def dijkstra(edges, start_id): num_node = len(edges) c = [[float('inf')] * 2 for _ in range(num_node)] #スタート地点以外の値は∞で初期化 c[start_id][0] = 0 #スタートは0で初期化 node_name = [] heapq.heappush(node_name, [0,0, start_id]) while len(node_name) > 0: #ヒープから取り出し _, ani, min_point = heapq.heappop(node_name) #経路の要素を各変数に格納することで,視覚的に見やすくする for factor in edges[min_point]: goal = factor[0] #終点 cost = factor[1] #コスト x = factor[2] # 動物がいるかどうか if ani == 1: if c[min_point][1] + cost < c[goal][1]: c[goal][1] = c[min_point][1] + cost heapq.heappush(node_name, [c[goal][1], 1, goal]) if ani == 0: if x == 0: if c[min_point][0] + cost < c[goal][0]: c[goal][0] = c[min_point][0] + cost if c[goal][0] < c[goal][1]: heapq.heappush(node_name, [c[goal][0], 0, goal]) if x == 1: if c[min_point][0] + cost < c[goal][1]: c[goal][1] = c[min_point][0] + cost heapq.heappush(node_name, [c[goal][1], 1, goal]) return c start_id = N-1 # UVC = [ [始点1,終点1,コスト1], ..., [始点M,終点M,コストM] ] UVC = [] for i in range(M): UVC.append([ABCX[i][0]-1, ABCX[i][1]-1, ABCX[i][2], ABCX[i][3]]) Di = False Edges = [[] for _ in range(N)] if Di: for ui, vi, ci in UVC: Edges[ui].append([vi,ci]) else: for ui, vi, ci, xi in UVC: Edges[ui].append([vi,ci,xi]) Edges[vi].append([ui,ci,xi]) opt = dijkstra(Edges, start_id) for i in range(N-1): print(opt[i][1])