import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.List
import Data.IntMap.Strict ((!))
import qualified Data.IntMap.Strict as M
import Data.Maybe
import qualified Data.Set as S
import qualified Data.Tuple as T

addToSet set xs = foldl' (\ acc x -> S.insert (T.swap x) acc) set xs

addToMap m xs = foldl' (\ acc (x, d) -> M.insert x d acc) m xs

dijkstra graph source = dijkstra' graph (S.singleton (0, source)) (M.singleton source 0)

dijkstra' graph pq res
    | S.null pq = res
    | otherwise = dijkstra' graph (addToSet pq' info) (addToMap res info)
    where
      fwd = M.findWithDefault
      ((dis, node), pq') = S.deleteFindMin pq
      nodes = fwd [] node graph
      update = filter (\ (x, e) -> M.notMember x res || dis + e < res!x) nodes
      info = map (\ (x, e) -> (x, dis + e)) update

buildGraph edges = 
 let g = groupBy (\ (x, _, _) (y, _, _) -> x == y) edges
 in M.fromDistinctAscList (map (\ x -> (f1 $ head x, map (\ (_, y, z) -> (y, z)) x)) g)

readInt = fst . fromJust . B.readInt
f1 (x, _, _) = x
f3 (_, _, x) = x

main = do
  ((nv,_,r):edges) <- map (\[x,y,z]->(x,y,z)) . map (map readInt . B.words) . B.lines <$> B.getContents
  let g = buildGraph (sort edges)
  let res = dijkstra g r
  mapM_ (putStrLn . maybe "INF" show . flip M.lookup res) [0..nv-1]