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

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

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 dist = dijkstra(adj, s);

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

auto dijkstra(Edge[][] adj, int s) {
    alias Pair = Tuple!(int, "num", int, "dist");
    immutable inf = 10^^9 + 7;

    int N = adj.length.to!int;

    auto dist = new int[](N);
    dist[] = inf;
    dist[s] = 0;

    auto q = BinaryHeap!(Array!Pair, "a.dist > b.dist")();
    q.insert(Pair(s, 0));

    int rep = N - 1;

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

        if (dist[v.num] < v.dist) continue;

        rep--;

        foreach (e ; adj[v.num]) {
            if (dist[e.to] > dist[v.num] + e.cost) {
                dist[e.to] = dist[v.num] + e.cost;
                q.insert(Pair(e.to, dist[e.to]));
            }
        }
    }

    return dist;
}



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);
        }
    }
}



struct Queue(T) {
    private {
        int N, head, tail;
        T[] data;
    }

    this(int n) {
        N = n + 1;
        data = new T[](N);
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return (tail + 1) % N == head;
    }

    T front() {
        return data[head];
    }

    void push(T x) {
        assert(!full);
        data[tail++] = x;
        tail %= N;
    }

    void pop() {
        assert(!empty);
        head = (head + 1) % N;
    }

    void clear() {
        head = tail = 0;
    }
}