import java.util.*

fun main(args: Array<String>) = p56()

fun p56() {
    val (v, e, r) = readLine()!!.split(' ').map { it.toInt() }

    data class Vertex(val index: Int, val neighbor: MutableSet<Pair<Int, Int>> = mutableSetOf())

    val vertexes = Array(v) { Vertex(it) }

    repeat(e) {
        val (s, t, d) = readLine()!!.split(' ').map { it.toInt() }
        vertexes[s].neighbor.add(t to d)
    }

    val queue = PriorityQueue<Pair<Vertex, Long>>(Comparator { o1, o2 -> o1.second.compareTo(o2.second) })
    queue.add(vertexes[r] to 0L)
    val inf = Long.MAX_VALUE / 10
    val distances = LongArray(v) { inf }
    distances[r] = 0

    while (queue.isNotEmpty()) {
        val (vertex, dist) = queue.poll()
        val (index, neighbor) = vertex
        if (dist > distances[index]) continue

        neighbor.forEach { (to, d) ->
            if (d + dist < distances[to]) {
                distances[to] = d + dist
                queue.add(vertexes[to] to d + dist)
            }
        }
    }

    val answer = distances.joinToString(System.lineSeparator()) { if (it != inf) it.toString() else "INF" }

    println(answer)
}

