class Node
  attr_accessor :name, :child, :done, :cost
  def initialize(i)
    @name = i
    @child = {}
    @done = false
    @cost = -1
  end
end

class Graph
  attr_accessor :node
  def initialize(v)
    @node = Array.new(v){|i| Node.new(i)}
  end
  def print
    @node.size.times{|i|
      if @node[i].cost > -1
        puts @node[i].cost
      else
        puts "INF"
      end
    }
  end
end
v, e, r = gets.split.map(&:to_i)

graph = Graph.new(v)

e.times{
  s, t, d = gets.split.map(&:to_i)
  graph.node[s].child[t] = d
}

graph.node[r].cost = 0

loop do
  doneNode = nil
  v.times{|i|
    next if graph.node[i].done || graph.node[i].cost < 0
    doneNode = graph.node[i] if doneNode == nil || graph.node[i].cost < doneNode.cost
  }
  break if doneNode == nil
  doneNode.done = true
  doneNode.child.each{|t, d|
    cost = doneNode.cost + d
    graph.node[t].cost = cost if graph.node[t].cost < 0 || cost < graph.node[t].cost
  }
end

graph.print