object Main {
    def main(args: Array[String]) = {
        val inf = Int.MaxValue-10000
        val in = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt)).toList
        val Array(v, _, r) = in.head
        val scanEdges = (g: Array[Int]) => in.tail.foldLeft(g)((a, b) => {
            if (a(b(0)) + b(2) < a(b(1))) a.updated(b(1), a(b(0)) + b(2))
            else a
        })
        def last(l: Stream[Array[Int]]): Array[Int] = {
            if (l.head sameElements l.tail.head) l.head
            else last(l.tail)
        }
        last(Stream.continually(scanEdges).scanLeft(Array.fill(v)(inf).updated(r, 0))((arr, f) => f(arr)))
            .foreach(n => if (n == inf) println("INF") else println(n))
    }
}