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
        var g = Array.fill(v)(inf).updated(r, 0)
        var flag = true
        while(flag) {
            var prev = g
            g = 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
            })
            flag = !(g sameElements prev)
        }
        g.foreach(n => if (n == inf) println("INF") else println(n))
    }
}