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).updated(r, 0)
        var flag = true
        while(flag) {
            var prev = g.clone
            for (Array(s, t, d) <- in.tail) if (g(s) + d < g(t)) g.update(t, g(s) + d)
            flag = !(g sameElements prev)
        }
        g.foreach(n => if (n == inf) println("INF") else println(n))
    }
}