using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Numerics;

class PriorityQueue<T> : IEnumerable<T>
{
    private readonly List<T> Heap;
    private readonly Comparison<T> compare;
    public int Count { get { return Heap.Count; } }

    public PriorityQueue() : this(Comparer<T>.Default.Compare) { }
    public PriorityQueue(Comparison<T> comparison)
    {
        Heap = new List<T>();
        compare = comparison;
    }

    public void Push(T item)
    {
        var i = Count;
        Heap.Add(item);
        while (i > 0)
        {
            var p = (i - 1) / 2;
            if (compare(Heap[p], item) <= 0)
            {
                break;
            }
            Heap[i] = Heap[p];
            i = p;
        }
        Heap[i] = item;
    }

    public T Pop()
    {
        var val = Heap[0];
        var x = Heap.Last();
        var i = 0;
        while (i * 2 + 1 < Count)
        {
            var a = i * 2 + 1;
            var b = i * 2 + 2;
            if (b < Count && compare(Heap[b], Heap[a]) < 0)
            {
                a = b;
            }
            if (compare(Heap[a], x) >= 0)
            {
                break;
            }
            Heap[i] = Heap[a];
            i = a;
        }
        Heap[i] = x;
        Heap.RemoveAt(Count - 1);
        return val;
    }

    public T Peek() { return Heap[0]; }

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        while (Count > 0)
        {
            yield return Pop();
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        while (Count > 0)
        {
            yield return Pop();
        }
    }
}

class MyClass
{
    public struct State : IComparable<State>
    {
        public State(int cost, int position, int preNode)
        {
            Cost = cost;
            Position = position;
            PreNode = preNode;
        }

        public int Cost { get; set; }
        public int Position { get; set; }
        public int PreNode { get; set; }

        public int CompareTo(State other)
        {
            if (Cost == other.Cost)
            {
                return Position.CompareTo(other.Position);
            }
            else
            {
                return Cost.CompareTo(other.Cost);
            }
        }
    }
    public struct Edge
    {
        public Edge(int node, int cost)
        {
            Node = node;
            Cost = cost;
        }

        public int Node { get; set; }
        public int Cost { get; set; }
    }

    public static int[] Dijkstra(IList<IList<Edge>> adj, int start)
    {
        var N = adj.Count;
        var dist = Enumerable.Repeat(int.MaxValue, N).ToArray();
        var pre_nodes = Enumerable.Range(0, N).ToArray();
        var heap = new PriorityQueue<State>();
        dist[start] = 0;
        heap.Push(new State(0, start, 0));
        while (heap.Count > 0)
        {
            var state = heap.Pop();
            var cost = state.Cost;
            var position = state.Position;
            var pre_node = state.PreNode;
            if (cost > dist[position])
            {
                continue;
            }
            pre_nodes[position] = pre_node;
            foreach (var edge in adj[position])
            {
                var next = new State(cost + edge.Cost, edge.Node, position);
                if (next.Cost < dist[next.Position])
                {
                    heap.Push(next);
                    dist[next.Position] = next.Cost;
                }
            }
        }
        return dist;
    }

    public static void Solve()
    {
        var input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var V = input[0];
        var E = input[1];
        var r = input[2];
        var adj = new List<Edge>[V];
        for (int i = 0; i < V; i++)
        {
            adj[i] = new List<Edge>();
        }
        for (int i = 0; i < E; i++)
        {
            input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var s = input[0];
            var t = input[1];
            var d = input[2];
            adj[s].Add(new Edge(t, d));
        }
        var dist = Dijkstra(adj, r);
        for (int i = 0; i < V; i++)
        {
            var d = dist[i];
            if (d == int.MaxValue)
            {
                Console.WriteLine("INF");
            }
            else
            {
                Console.WriteLine(d);
            }
        }
    }

    public static void Main()
    {
        var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }
}



Measured memory_bytes: 39900