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

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

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

  until pq.empty?
    v_cost, v = deq(pq)
    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
        enq(pq, [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