using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using static System.Console;
using static System.Math;

class Program
{
    static void Main()
    {
        SetOut(new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        var n = FastIO.Int();
        var m = FastIO.Int();
        var r = FastIO.Int();
        var sp = new ShortestPath(n);
        for (int i = 0; i < m; i++)
        {
            var s = FastIO.Int();
            var t = FastIO.Int();
            var d = FastIO.Int();
            sp.Add(s, t, d);
        }
        foreach (var ans in sp.Dijkstra(r)) WriteLine(ans != long.MaxValue ? ans.ToString() : "INF");
        Out.Flush();
    }
}

public static class FastIO
{
    static System.IO.Stream str = System.Console.OpenStandardInput();
    const int size = 1024;
    static byte[] buffer = new byte[size];
    static int ptr;
    static int len;

    static byte Read()
    {
        if (ptr == len)
        {
            len = str.Read(buffer, 0, size);
            if (len == 0) return 0;
            ptr = 0;
        }
        return buffer[ptr++];
    }

    public static int Int()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }

    public static long Long()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0L;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }
}

struct Edge
{
    public int to;
    public int dist;
}

class ShortestPath
{
    List<Edge>[] graph;
    int E;

    public ShortestPath(int V)
    {
        graph = new List<Edge>[V];
        for (int i = 0; i < graph.Length; i++)
        {
            graph[i] = new List<Edge>();
        }
    }

    public void Add(int from, int to, int dist)
    {
        graph[from].Add(new Edge { to = to, dist = dist });
        E++;
    }

    public long[] Dijkstra(int s)//LongとかDoubleとか
    {
        var q = new PriorityQueue<int>(E);
        q.Push(0, s);
        var distance = new long[graph.Length];//ダイクストラはlongになりえる
        for (int i = 0; i < distance.Length; i++)//!
        {
            distance[i] = long.MaxValue;
        }
        distance[s] = 0;
        while (q.Count != 0)
        {
            var v = q.Pop();
            if (distance[v.Value] < v.Priority) continue;
            foreach (var e in graph[v.Value])
            {
                if (distance[e.to] > distance[v.Value] + e.dist)
                {
                    distance[e.to] = distance[v.Value] + e.dist;
                    q.Push(distance[e.to], e.to);//この時点で枝刈りしておこう
                }
            }
        }
        return distance;
    }
}

public struct PriorityValuePair<TValue>//TKeyも指定したいが　型クラスはいったらやることissueに書くか
{
    public long Priority;
    public TValue Value;

    public void Deconstruct(out long priority, out TValue value)
    {
        priority = Priority;
        value = Value;
    }
}

public class PriorityQueue<T>
{
    PriorityValuePair<T>[] buffer;
    int count;
    public int Count => count;

    public PriorityQueue() : this(1024)
    {

    }

    public PriorityQueue(int capacity)
    {
        buffer = new PriorityValuePair<T>[Math.Max(capacity, 1024)];
    }

    void Expand()
    {
        var b = new PriorityValuePair<T>[buffer.Length * 2];
        Array.Copy(buffer, 0, b, 0, buffer.Length);
        buffer = b;
    }

    public void Push(long priority, T item)
    {
        if (count == buffer.Length) Expand();
        var v = new PriorityValuePair<T> { Priority = priority, Value = item };
        var i = count++;
        while (i > 0)
        {
            var p = (i - 1) / 2;//シフトにしたほうが速いか
            if (buffer[p].Priority <= v.Priority) break;
            buffer[i] = buffer[p];
            i = p;
        }
        buffer[i] = v;
    }

    public PriorityValuePair<T> Pop()
    {
        var ret = buffer[0];
        var x = buffer[--count];
        var p = 0;
        while (p * 2 + 1 < count)
        {
            var a = p * 2 + 1;
            if (a + 1 < count && buffer[a + 1].Priority < buffer[a].Priority) a++;
            if (buffer[a].Priority >= x.Priority) break;
            buffer[p] = buffer[a];
            p = a;
        }
        buffer[p] = x;
        return ret;
    }
}


Measured memory_bytes: 35496