Infinity = 2 << 33

def dijkstra graph, s
  n = graph.size
  dist = Array.new(n, Infinity)
  used = Array.new(n, false)
  dist[s] = 0
  used[s] = true
  q = Array.new
  q << s

  until q.empty?
    u = q.delete_at(0)
    graph[u].each do |edge|
      v = edge[0]
      w = edge[1]
      total = dist[u] + w
      if total < dist[v]
        dist[v] = total
        q << v
      end
    end
  end

  dist
end

v,e,r = $stdin.gets.chomp.split(" ").map(&:to_i)
graph = Array.new(v).map{Array.new}
e.times do
  s,t,d = $stdin.gets.chomp.split(" ").map(&:to_i)
  graph[s] << [t, d]
end
dist = dijkstra(graph, r)
dist.each do |cost|
  puts(cost == Infinity)? "INF" : cost
end