# ?????????????????????????????????????§???????????????????????????????
# @param [Array]   g Edge ?????????????????§??¨???????????°??????
# @param [Integer] s ????§????????????????
# @return [Array]  ?????????????????§???????¨?????????????????´????????????????
# g ??????????????????????????§?????????
# g_i ?????? i ???????????????????????????????????¨??????????????????????????¨??????????´?????????????
class PriorityQueue
  def initialize
    @a = [:snt]
  end

  def empty?
    @a.size == 1
  end

  def enq(val)
    @a << val
    cd = @a.size - 1
    pr = cd / 2
    until pr.zero? || (@a[pr] <=> @a[cd]) < 0
      @a[pr], @a[cd] = @a[cd], @a[pr]
      cd = pr
      pr = cd / 2
    end
  end

  def deq
    rs = @a[1]
    @a[1] = @a[-1]
    @a.delete_at(-1)
    if @a.size > 1
      pr = 1
      cd = [pr * 2, pr * 2 + 1].select { |i| @a[i] }.min_by { |i| @a[i] }
      until cd.nil? || (@a[pr] <=> @a[cd]) < 0
        @a[pr], @a[cd] = @a[cd], @a[pr]
        pr = cd
        cd = [pr * 2, pr * 2 + 1].select { |i| @a[i] }.min_by { |i| @a[i] }
      end
    end
    rs
  end
end

def dijkstra(g, s)
  que = PriorityQueue.new
  d = Array.new(g.size) { Float::INFINITY }
  d[s] = 0
  que.enq [0, s]

  until que.empty?
    v_cost, v = que.deq
    next if d[v] < v_cost
    g[v].each do |e|
      if d[e.to] > d[v] + e.cost
        d[e.to] = d[v] + e.cost
        que.enq [d[e.to], e.to]
      end
    end
  end

  d
end

Edge = Struct.new('Edge', :to, :cost)

v, e, r = gets.split.map(&:to_i)
g = Array.new(v) { [] }
e.times do
  s, t, d = gets.split.map(&:to_i)
  g[s] << Edge.new(t, d)
end

dijkstra(g, r).each do |sum|
  puts (sum == Float::INFINITY ? 'INF' : sum)
end