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 g = Array.fill(v)(inf)
        g(r) = 0
        var flag = true
        while(flag) {
            flag = false
            for (b <- in.tail) if (g(b(0)) + b(2) < g(b(1))) {g(b(1)) = g(b(0)) + b(2); flag = true}
        }
        g.foreach(n => if (n == inf) println("INF") else println(n))
    }
}