import qualified Data.Set as S import qualified Data.IntMap as M import qualified Data.ByteString.Char8 as B import qualified Data.Array.IArray as A import Data.Maybe type Vertex = Int type Weight = Int type Edge = (Vertex, (Vertex, Weight)) type Path = [Vertex] type Graph = A.Array Vertex (S.Set (Vertex, Weight)) type Memo = M.IntMap (Weight, Path) type PriorityQueue = S.Set (Weight, Vertex) empty = S.empty isEmpty = S.null singleton = S.singleton insert = S.insert deleteFindMin = S.deleteFindMin readUndirectedEdge :: B.ByteString -> [Edge] readUndirectedEdge = concatMap ((\[v,w,l] -> [(v,(w,l)),(w,(v,l))]) . map readVertex . B.words) . B.lines readVertex :: B.ByteString -> Vertex readVertex = fst . fromJust . B.readInt buildG :: Int -> [Edge] -> Graph buildG n = A.accumArray (flip S.insert) S.empty (0, n-1) from :: Graph -> Vertex -> S.Set (Vertex, Weight) from g = (g A.!) main :: IO () main = do [n,_,vs,vg] <- map read . words <$> getLine es <- readUndirectedEdge <$> B.getContents let g = buildG n es putStrLn $ unwords $ map show (route g vs vg) route :: Graph -> Vertex -> Vertex -> Path route g vs vg = vs : r where (_,r) = imr M.! vs imr = dijkstra g vg -- accumrated weight and lexical order shortest path dijkstra :: Graph -> Vertex -> Memo dijkstra g s = dijkstraCore g q0 m0 where q0 = singleton (0, s) m0 = M.singleton s (0, []) dijkstraCore :: Graph -> PriorityQueue -> Memo -> Memo dijkstraCore g q m | isEmpty q = m | otherwise = dijkstraCore g q2 m1 where ((w0, s), q1) = deleteFindMin q p = s : snd (m M.! s) m1 = S.foldr (\(t,w) acc -> M.insertWith min t (w0+w,p) acc) m vns q2 = S.foldr (\(t,w) acc -> insert (w0+w, t) acc) q1 vns vns = S.filter (\(t,w) -> M.notMember t m || w0+w <= fst (m M.! t)) (from g s)