import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable inf = 10^^9 + 7;
alias Edge = Tuple!(int, "to", int, "cost");

void main() {
    int n, m, s;
    scan(n, m, s);

    auto adj = new Edge[][](n, 0);

    foreach (i ; 0 .. m) {
        int a, b, c;
        scan(a, b, c);
        adj[a] ~= Edge(b, c);
    }

    auto d = dijkstra(adj, s);

    foreach (i ; 0 .. n) {
        if (d[i] == inf) writeln("INF");
        else writeln(d[i]);
    }
}

int[] dijkstra(Edge[][] adj, int s) {
    alias Node = Tuple!(int, "num", int, "dist");

    auto n = adj.length.to!int;
    auto minhp = new BinaryHeap!(Array!Node, "a.dist > b.dist")();
    auto d = new int[](n);
    d[] = inf;
    minhp.insert(Node(s, 0));

    int rep = n;

    while (rep && !minhp.empty) {
        auto v = minhp.front;
        minhp.popFront();

        if (d[v.num] <= v.dist) continue;
        d[v.num] = v.dist;
        rep--;

        foreach (e ; adj[v.num]) {
            if (d[v.num] + e.cost < d[e.to]) {
                minhp.insert(Node(e.to, d[v.num] + e.cost));
            }
        }
    }

    return d;
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}