N, M = gets.split(" ").map{|s| s.to_i} cost = Array.new(N) {Array.new} M.times { a, b, c, x = gets.split(" ").map{|s| s.to_i} cost[a-1] << [b-1, c, x] cost[b-1] << [a-1, c, x] } d = Array.new(N) {Array.new(2, Float::INFINITY)} d[N-1][0] = 0 q = [[N-1, 0]] while q.length > 0 do v, x = q.shift cost[v].each {|nv, c, nx| if nx == 0 then if d[nv][x] > d[v][x] + c then d[nv][x] = d[v][x] + c q << [nv, x] end else if d[nv][nx] > d[v][x] + c then d[nv][nx] = d[v][x] + c q << [nv, nx] end end } end 0.upto(N-2) {|i| puts d[i][1] }