{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Monad
import Control.Applicative
import Control.Monad.State
import Control.Monad.ST
import Data.List
import Data.Array
import Data.Array.IO
import Data.Array.ST
import Data.Maybe
import Data.Bits
import Data.Ord
import Data.Ratio
import Data.Int
import Data.Char
import qualified Data.ByteString.Char8 as BS
import qualified Data.Set as Set
import Text.Printf
import Debug.Trace
import Numeric

modifyArray arr i f = readArray arr i >>= \x -> writeArray arr i (f x)

listToTuple2 [a,b] = (a,b)
listToTuple3 [a,b,c] = (a,b,c)
listToTuple4 [a,b,c,d] = (a,b,c,d)

getWords = getLine >>= return . words

readIntBS = fst . fromJust . BS.readInt

for = flip map

main :: IO ()
main = do
	(v,e,r) <- listToTuple3 . map read . words <$> getLine :: IO (Int,Int,Int)
	es <- replicateM e $ listToTuple3 . map readIntBS . BS.words <$> BS.getLine :: IO [(Int,Int,Int)]
	
	let
		graph = buildWeightedDirectedGraph (0,v-1) es
		dijk = dijkstra graph r
	
	mapM_ putStrLn $ for [0..v-1] $ \i -> if dijk!i /= inf then show (dijk!i) else "INF"

  


{-- 重み付き有向グラフ --}
buildWeightedDirectedGraph :: (Node,Node) -> [(Node,Node,Cost)] -> Array Node [(Node,Cost)]
buildWeightedDirectedGraph bound dat = runST $ do
  graph <- newArray bound [] :: ST s (STArray s Node [(Node,Cost)])
  forM_ dat $ \(s,e,c) -> do
    modifyArray graph s ((e,c):)
  freeze graph

type Node = Int
type Cost = Int
type Edge = (Node, Cost)
type Graph = Array Node [Edge]
type Route = [Edge]


inf = maxBound :: Int

dijkstra :: Graph -> Node -> Array Node Cost
dijkstra graph start = runST $ do
  cost <- newArray (bounds graph) inf :: ST s (STUArray s Node Cost)
  check <- newArray (bounds graph) False :: ST s (STUArray s Node Bool)
  writeArray cost start 0
  evalStateT (dijkstra' cost check) (Set.singleton (0, start))
  freeze cost
  where
    dijkstra' cost check = do
      isNull <- Set.null <$> get
      when (not $ isNull) $ do
        (curCost, cur) <- state Set.deleteFindMin
        let nextNodes = graph ! cur

        lift $ writeArray check cur True

        forM_ nextNodes $ \(next, edgeCost) -> do
          nextCost <- lift $ readArray cost next
          
          if nextCost == -1 then do
            lift $ writeArray cost next (curCost + edgeCost)
            modify $ Set.insert (curCost + edgeCost, next)
            else do
            when (curCost + edgeCost < nextCost) $ lift $ writeArray cost next (curCost + edgeCost)
            isChecked <- lift $ readArray check next
            unless isChecked $ do
              (\x -> modify $ \s -> Set.insert (x, next) $ Set.delete (nextCost, next) s) =<< (lift $ readArray cost next)
        dijkstra' cost check

