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

namespace AOJ
{
    class Program
    {
        static void Main(string[] args)
        {
            var firstLine = ReadLine().Split(' ').Select(value => int.Parse(value)).ToArray();
            var v = firstLine[0];
            var e = firstLine[1];
            var r = firstLine[2];
            var edges = new Dictionary<int, List<int[]>>();
            var costs = new int[v];
            var isFixed = new bool[v];

            for (var i = 0; i < v; i++)
            {
                edges.Add(i, new List<int[]>());
                costs[i] = int.MaxValue;
            }
            for(var i = 0; i < e; i++)
            {
                var line = ReadLine().Split(' ').Select(value => int.Parse(value)).ToArray();
                edges[line[0]].Add(new int[2] { line[1], line[2] });
            }

            costs[r] = 0;
            var queue = new PriorityQueue(10 * v);
            queue.Enqueue(r, costs[r]);

            while(queue.Count > 0)
            {
                var node = queue.Dequeue();
                if (isFixed[node])
                {
                    continue;
                }
                var destinations = edges[node];
                isFixed[node] = true;
                v--;

                foreach(var destination in destinations)
                {
                    if(!isFixed[destination[0]] && costs[destination[0]] > costs[node] + destination[1])
                    {
                        costs[destination[0]] = costs[node] + destination[1];
                        queue.Enqueue(destination[0], costs[destination[0]]);
                    }
                }

                if(v == 0)
                {
                    break;
                }
            }

            foreach(var cost in costs)
            {
                WriteLine(cost != int.MaxValue ? $"{cost}" : "INF");
            }
        }

        private class PriorityQueue
        {
            private int[][] heap;


            public int Count { get; private set; } = 0;

            public PriorityQueue(int size)
            {
                this.heap = new int[size][];
            }

            public void Enqueue(int node, int distance)
            {
                if(this.Count == 0)
                {
                    heap[0] = new int[2] { node, distance };
                    this.Count++;
                    return;
                }

                var nodeNo = this.Count;

                while(nodeNo > 0)
                {
                    var parentNo = (nodeNo - 1) / 2;
                    if(distance >= heap[parentNo][1])
                    {
                        break;
                    }
                    heap[nodeNo] = heap[parentNo];
                    nodeNo = parentNo;
                }

                heap[nodeNo] = new int[2] { node, distance };
                this.Count++;
            }

            public int Dequeue()
            {
                this.Count--;

                var node = heap[0];
                var last = heap[this.Count];

                var parentNo = 0;
                while(parentNo * 2 + 1 < this.Count)
                {
                    var leftChild = parentNo * 2 + 1;
                    var rightChild = parentNo * 2 + 2;

                    if(rightChild < this.Count && heap[leftChild][1] > heap[rightChild][1])
                    {
                        leftChild = rightChild;
                    }

                    if(heap[leftChild][1] > last[1])
                    {
                        break;
                    }

                    heap[parentNo] = heap[leftChild];
                    parentNo = leftChild;
                }

                heap[parentNo] = last;

                return node[0];
            }
        }
    }
}



Measured memory_bytes: 62036