Language: C++

{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.Primitive
import           Data.Bits
import qualified Data.ByteString.Char8       as B
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import           Data.Function
import           Data.Primitive.MutVar
import qualified Data.Vector                 as V
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM

main :: IO ()
main = do
    [v, e, r] <- map read.words <$> getLine
    edges <- U.unfoldrN e (readInt3.B.dropWhile isSpace) <$> B.getContents
    putStr.unlines.map showCost.U.toList.solve e r $ fromEdges v edges

solve :: Int -> Vertex -> Graph -> U.Vector Cost
solve numE root gr = dijkstra numE root gr

showCost :: Cost -> String
showCost cost
    | cost < inf = show cost
    | otherwise = "INF"

-------------------------------------------------------------------------------

readInt3 :: B.ByteString -> Maybe ((Int,Int,Int), B.ByteString)
readInt3 bs = Just ((x,y,z),bsz)
  where
    Just (x, bsx) = B.readInt bs
    Just (y, bsy) = B.readInt $ B.unsafeTail bsx
    Just (z, bsz) = B.readInt $ B.unsafeTail bsy

type Vertex = Int
type Cost = Int
type Edge = (Vertex, Vertex, Cost)
type Graph = V.Vector (U.Vector (Vertex, Cost))

fromEdges :: Int -> U.Vector Edge -> Graph
fromEdges numV edges = V.map U.fromList
    . V.unsafeAccumulate (flip (:)) (V.replicate numV [])
    . U.convert
    . U.map (\(src, dst, cost) -> (src, (dst, cost)))
    $ edges

inf :: Cost
inf = 0x3f3f3f3f

dijkstra :: Int -> Vertex -> Graph -> U.Vector Cost
dijkstra numE root gr = U.create $ do
    dist <- UM.replicate (V.length gr) inf
    heap <- newHeap numE
    UM.unsafeWrite dist root 0
    insertM (0, root) heap
    fix $ \loop -> do
        hd <- deleteFindMinM heap
        case hd of
            Just (!d,!v) -> do
                dv <- UM.unsafeRead dist v
                if d > dv
                then loop
                else do
                    U.forM_ (V.unsafeIndex gr v) $ \(nv, v2nv) -> do
                        let !dnv = dv + v2nv
                        old <- UM.unsafeRead dist nv
                        when (dnv < old) $ do
                            UM.unsafeWrite dist nv dnv
                            insertM (dnv, nv) heap
                    loop
            Nothing -> return dist

data HeapM m a = HeapM (MutVar m Int) (UM.MVector m a)

newHeap :: (PrimMonad m, U.Unbox a) => Int -> m (HeapM (PrimState m) a)
newHeap limitSize = HeapM `liftM` newMutVar 0 `ap` UM.new limitSize

getHeapSize :: (PrimMonad m) => HeapM (PrimState m) a -> m Int
getHeapSize (HeapM ref _) = readMutVar ref
{-# INLINE getHeapSize #-}

insertM :: (PrimMonad m, U.Unbox a, Ord a)
        => a -> HeapM (PrimState m) a -> m ()
insertM x heap@(HeapM ref vec) = do
    size <- getHeapSize heap
    modifyMutVar ref (+1)
    flip fix size $ \loop !i ->
        if i == 0
        then UM.unsafeWrite vec 0 x
        else do
            let !parent = (i - 1) `unsafeShiftR` 1
            p <- UM.unsafeRead vec parent
            if p <= x
            then UM.unsafeWrite vec i x
            else do
                UM.unsafeWrite vec i p
                loop parent
{-# INLINE insertM #-}

unsafeDeleteMinM :: (PrimMonad m, U.Unbox a, Ord a)
                 => HeapM (PrimState m) a -> m ()
unsafeDeleteMinM (HeapM ref vec) = do
    modifyMutVar' ref (subtract 1)
    size <- readMutVar ref
    x <- UM.unsafeRead vec size
    flip fix 0 $ \loop !i -> do
        let !l = unsafeShiftL i 1 .|. 1
        if size <= l
        then UM.unsafeWrite vec i x
        else do
            let !r = l + 1
            childL <- UM.unsafeRead vec l
            childR <- UM.unsafeRead vec r
            if r < size && childR < childL
            then if x <= childR
                 then UM.unsafeWrite vec i x
                 else do
                     UM.unsafeWrite vec i childR
                     loop r
            else if x <= childL
                 then UM.unsafeWrite vec i x
                 else do
                     UM.unsafeWrite vec i childL
                     loop l
{-# INLINE unsafeDeleteMinM #-}

unsafeMinViewM :: (PrimMonad m, U.Unbox a) => HeapM (PrimState m) a -> m a
unsafeMinViewM (HeapM _ vec) = UM.unsafeRead vec 0
{-# INLINE unsafeMinViewM #-}

minViewM :: (PrimMonad m, U.Unbox a) => HeapM (PrimState m) a -> m (Maybe a)
minViewM heap = do
    size <- getHeapSize heap
    if size > 0
    then Just `liftM` unsafeMinViewM heap
    else return Nothing
{-# INLINE minViewM #-}

deleteFindMinM :: (PrimMonad m, U.Unbox a, Ord a)
               => HeapM (PrimState m) a -> m (Maybe a)
deleteFindMinM heap = do
    size <- getHeapSize heap
    if size > 0
    then liftM2 ((Just.).const) (unsafeMinViewM heap) (unsafeDeleteMinM heap)
    else return Nothing
{-# INLINE deleteFindMinM #-}

clearHeapM :: (PrimMonad m) => HeapM (PrimState m) a -> m ()
clearHeapM (HeapM ref _) = writeMutVar ref 0
{-# INLINE clearHeapM #-}

Compiler output:
a.cc:1:3: error: stray '#' in program
    1 | {-# OPTIONS_GHC -O2 #-}
      |   ^
a.cc:1:21: error: stray '#' in program
    1 | {-# OPTIONS_GHC -O2 #-}
      |                     ^
a.cc:2:3: error: stray '#' in program
    2 | {-# LANGUAGE BangPatterns #-}
      |   ^
a.cc:2:27: error: stray '#' in program
    2 | {-# LANGUAGE BangPatterns #-}
      |                           ^
a.cc:49:14: error: stray '\' in program
   49 |     . U.map (\(src, dst, cost) -> (src, (dst, cost)))
      |              ^
a.cc:61:11: error: stray '\' in program
   61 |     fix $ \loop -> do
      |           ^
a.cc:69:52: error: stray '\' in program
   69 |                     U.forM_ (V.unsafeIndex gr v) $ \(nv, v2nv) -> do
      |                                                    ^
a.cc:81:27: error: stray '`' in program
   81 | newHeap limitSize = HeapM `liftM` newMutVar 0 `ap` UM.new limitSize
      |                           ^
a.cc:81:33: error: stray '`' in program
   81 | newHeap limitSize = HeapM `liftM` newMutVar 0 `ap` UM.new limitSize
      |                                 ^
a.cc:81:47: error: stray '`' in program
   81 | newHeap limitSize = HeapM `liftM` newMutVar 0 `ap` UM.new limitSize
      |                                               ^
a.cc:81:50: error: stray '`' in program
   81 | newHeap limitSize = HeapM `liftM` newMutVar 0 `ap` UM.new limitSize
      |                                                  ^
a.cc:85:3: error: stray '#' in program
   85 | {-# INLINE getHeapSize #-}
      |   ^
a.cc:85:24: error: stray '#' in program
   85 | {-# INLINE getHeapSize #-}
      |                        ^
a.cc:89:15: error: stray '@' in program
   89 | insertM x heap@(HeapM ref vec) = do
      |               ^
a.cc:92:21: error: stray '\' in program
   92 |     flip fix size $ \loop !i ->
      |                     ^
a.cc:96:35: error: stray '`' in program
   96 |             let !parent = (i - 1) `unsafeShiftR` 1
      |                                   ^
a.cc:96:48: error: stray '`' in program
   96 |             let !parent = (i - 1) `unsafeShiftR` 1
      |                                                ^
a.cc:103:3: error: stray '#' in program
  103 | {-# INLINE insertM #-}
      |   ^
a.cc:103:20: error: stray '#' in program
  103 | {-# INLINE insertM #-}
      |                    ^
a.cc:108:17: warning: missing terminating ' character
  108 |     modifyMutVar' ref (subtract 1)
      |                 ^
a.cc:108:17: error: missing terminating ' character
  108 |     modifyMutVar' ref (subtract 1)
      |                 ^~~~~~~~~~~~~~~~~~
a.cc:111:18: error: stray '\' in program
  111 |     flip fix 0 $ \loop !i -> do
      |                  ^
a.cc:130:3: error: stray '#' in program
  130 | {-# INLINE unsafeDeleteMinM #-}
      |   ^
a.cc:130:29: error: stray '#' in program
  130 | {-# INLINE unsafeDeleteMinM #-}
      |                             ^
a.cc:134:3: error: stray '#' in program
  134 | {-# INLINE unsafeMinViewM #-}
      |   ^
a.cc:134:27: error: stray '#' in program
  134 | {-# INLINE unsafeMinViewM #-}
      |                           ^
a.cc:140:15: error: stray '`' in program
  140 |     then Just `liftM` unsafeMinViewM heap
      |               ^
a.cc:140:21: error: stray '`' in program
  140 |     then Just `liftM` unsafeMinViewM heap
      |                     ^
a.cc:142:3: error: stray '#' in program
  142 | {-# INLINE minViewM #-}
      |   ^
a.cc:142:21: error: stray '#' in program
  142 | {-# INLINE minViewM #-}
      |                     ^
a.cc:151:3: error: stray '#' in program
  151 | {-# INLINE deleteFindMinM #-}
      |   ^
a.cc:151:27: error: stray '#' in program
  151 | {-# INLINE deleteFindMinM #-}
      |                           ^
a.cc:155:3: error: stray '#' in program
  155 | {-# INLINE clearHeapM #-}
      |   ^
a.cc:155:23: error: stray '#' in program
  155 | {-# INLINE clearHeapM #-}
      |                       ^
a.cc:1:1: error: expected unqualified-id before '{' token
    1 | {-# OPTIONS_GHC -O2 #-}
      | ^
a.cc:2:1: error: expected unqualified-id before '{' token
    2 | {-# LANGUAGE BangPatterns #-}
      | ^
a.cc:4:1: error: 'import' does not name a type
    4 | import           Control.Applicative
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:87:1: error: 'insertM' does not name a type
   87 | insertM :: (PrimMonad m, U.Unbox a, Ord a)
      | ^~~~~~~
a.cc:105:1: error: 'unsafeDeleteMinM' does not name a type
  105 | unsafeDeleteMinM :: (PrimMonad m, U.Unbox a, Ord a)
      | ^~~~~~~~~~~~~~~~
a.cc:132:1: error: 'unsafeMinViewM' does not name a type
  132 | unsafeMinViewM :: (PrimMonad m, U.Unbox a) => HeapM (PrimState m) a -> m a
      | ^~~~~~~~~~~~~~
a.cc:136:1: error: 'minViewM' does not name a type
  136 | minViewM :: (PrimMonad m, U.Unbox a) => HeapM (PrimState m) a -> m (Maybe a)
      | ^~~~~~~~
a.cc:144:1: error: 'deleteFindMinM' does not name a type
  144 | deleteFindMinM :: (PrimMonad m, U.Unbox a, Ord a)
      | ^~~~~~~~~~~~~~
a.cc:153:1: error: 'clearHeapM' does not name a type
  153 | clearHeapM :: (PrimMonad m) => HeapM (PrimState m) a -> m ()
      | ^~~~~~~~~~