class Edge
  attr_accessor :s, :t, :d
  def initialize(arr)
    self.s = arr[0]
    self.t = arr[1]
    self.d = arr[2]
  end
end

v, e, r = gets.strip.split(' ').map(&:to_i)

es = []

e.times do
  edge = Edge.new(gets.strip.split(' ').map(&:to_i))
  es << edge
end

d = Array.new(v, Float::INFINITY)
d[r] = 0;

loop do
  update = false
  e.times do |i|
    edge = es[i]
    if d[edge.s] == Float::INFINITY
      next
    end
    dd = d[edge.s] + edge.d
    if d[edge.t] > dd
      d[edge.t] = dd
      update = true
    end
  end

  break unless update
end

d.each do |i|
  if i == Float::INFINITY
    puts "INF"
  else
    puts i
  end
end