Infinity = 2 << 33

def getMin q
  min = Infinity
  u = -1
  target = -1
  q.size().times do |i|
    if q[i][1] < min
      min = q[i][1]
      u = q[i][0]
      target = i
    end
  end
  q.delete_at target
  u
end

def decreaseKey q, v, w
  q.size.times do |i|
    if q[i][0] == v
      q[i][1] = w
      break
    end
  end
end

def dijkstra graph, s
  n = graph.size
  dist = Array.new(n, Infinity)
  dist[s] = 0
  q = Array.new
  n.times do |i|
    q << [i, dist[i]]
  end

  until q.empty?
    u = getMin q
    graph[u].each do |edge|
      v = edge[0]
      w = edge[1]
      total = dist[u] + w
      if total < dist[v]
        dist[v] = total
        decreaseKey q,v,total
      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