object Main{
  class Edge(val from:Int,val to:Int,val cost:Long){}

  // v:????????°
  // s:?§????
  // graph:??£??\?????????
  def dijkstra(v:Int,s:Int,graph:Array[Array[Edge]],inf:Long= Long.MaxValue / 10) : Array[Long] = {
    val d : Array[Long] = Array.fill(v)(inf)
    val que = new scala.collection.mutable.PriorityQueue[Tuple2[Long,Int]]()(Ordering.Tuple2[Long,Int].reverse)
    que += Tuple2(0,s)
    d(s) = 0
    while(que.nonEmpty){
      val (cost,node) = que.dequeue
      if(d(node) >= cost){
        for(e <- graph(node)){
          if(d(e.to) > d(node)+e.cost){
            d(e.to) = d(node)+e.cost
            que+=Tuple2(d(e.to),e.to)
          }
        }
      }
    }
    return d
  }

  def main(args : Array[String]){
    val inf = Long.MaxValue / 10
    val sc = new java.util.Scanner(System.in)
    val v,e,r = sc.nextInt
    val g : Array[Array[Edge]] = Array.fill(v)(Array.empty)

    for(i <- 0 to e-1){
      val s,t,d = sc.nextInt
      g(s) :+= new Edge(s,t,d)
    }

    val res = dijkstra(v,r,g,inf)
    res.foreach(x => if(x==inf) println("INF") else println(x))
  }
}