Problem:
Single Source Shortest Path

For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).

Input

An edge-weighted graph G (V, E) and the source r.

|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1

|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output

Print the costs of SSSP in the following format.

c0
c1
:
c|V|-1

The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.

Constraints

1 ≤ |V| ≤ 100000

0 ≤ di ≤ 10000

0 ≤ |E| ≤ 500000

There are no parallel edges

There are no self-loops

Sample Input 1

4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5

Sample Output 1

0
1
3
4

Sample Input 2

4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5

Sample Output 2

3
0
2
INF

Solution in Haskell (judge verdict: Accepted)

{-# 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 #-}