using System;
using System.IO;
using System.Collections.Generic;

namespace GPL1A_3
{
    public class Program
    {
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var V = ss.Next(int.Parse);
            var E = ss.Next(int.Parse);
            var R = ss.Next(int.Parse);

            var dj = new SPFA(V);
            for (var i = 0; i < E; i++)
            {
                var S = ss.Next(int.Parse);
                var T = ss.Next(int.Parse);
                var D = ss.Next(long.Parse);
                dj.AddEdge(S, T, D);
            }

            foreach (var dist in dj.GetDistances(R))
            {
                sw.WriteLine(dist == SP.Infinity ? "INF" : dist.ToString());
            }
            //---------------------------------
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            new Program().Solve(ss, sw);
            sw.Flush();
        }

        static readonly Func<string, string> String = s => s;
    }

    public abstract class SP
    {
        public static readonly long Infinity = long.MaxValue;
        public static readonly long[] NegativeCycle = null;

        protected struct Edge
        {
            public int To;
            public long Cost;
        }

        protected int V { get; }
        protected List<Edge>[] Edges { get; }

        protected SP(int v)
        {
            V = v;
            Edges = new List<Edge>[v];
            for (var i = 0; i < v; i++) Edges[i] = new List<Edge>();
        }

        public void AddEdge(int from, int to, long cost)
        {
            Edges[from].Add(new Edge {To = to, Cost = cost});
        }

        public void AddUndirectedEdge(int v1, int v2, long cost)
        {
            AddEdge(v1, v2, cost);
            AddEdge(v2, v1, cost);
        }

        public long[] GetDistances(int start)
        {
            var dist = new long[V];
            for (var i = 0; i < V; i++) dist[i] = Infinity;
            dist[start] = 0;
            dist = GetDistancesCore(start, dist);
            return dist;
        }

        protected abstract long[] GetDistancesCore(int start, long[] dist);
    }

    public class SPFA : SP
    {
        class SPFAQueue
        {
            readonly int v;
            readonly Queue<int> que;
            readonly bool[] isIns;
            readonly int[] pushCounts;

            public int Count { get { return que.Count; } }

            public SPFAQueue(int v)
            {
                this.v = v;
                que = new Queue<int>();
                isIns = new bool[v];
                pushCounts = new int[v];
            }

            public bool Enqueue(int n)
            {
                if (isIns[n]) return true;
                isIns[n] = true;
                que.Enqueue(n);
                return (++pushCounts[n]) < v;
            }

            public int Dequeue()
            {
                var ret = que.Dequeue();
                isIns[ret] = false;
                return ret;
            }
        }

        public SPFA(int v) : base(v) { }

        protected override long[] GetDistancesCore(int start, long[] dist)
        {
            var que = new SPFAQueue(V);
            que.Enqueue(start);

            while (que.Count > 0)
            {
                var from = que.Dequeue();
                foreach (var edge in Edges[from])
                {
                    if (dist[from] == Infinity || dist[edge.To] <= dist[from] + edge.Cost) continue;
                    dist[edge.To] = dist[from] + edge.Cost;

                    if (que.Enqueue(edge.To)) continue;
                    return NegativeCycle;
                }
            }

            return dist;
        }
    }

    public class StreamScanner
    {
        static readonly char[] Sep = {' '};
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}


Measured memory_bytes: 38400