import sequtils,strutils proc `<`(A, B : openarray[int]) : bool= var i : int while true: if A[i] < B[i]: return false elif A[i] > B[i]: return true else: i += 1 proc `<=`(A, B : openarray[int]) : bool = return A < B type priorityQue[I:static[int];T:tuple] = array[I + 1,T] item = tuple[id : int; cost : seq[int]] dist = tuple[s, d : int] proc push[T](A:var priorityQue; b: T;)= const key = 1 A[0][0] += 1 A[A[0][0]] = b # A[0][0]はsize、最大のIndex var index = A[0][0] while index > 1 and A[index][key] > A[index div 2][key]: (A[index],A[index div 2]) = (A[index div 2],A[index]) index = index div 2 proc pop(A: var priorityQue):item = const key = 1 result = A[1] A[1] = A[A[0][0]] var a : item A[A[0][0]] = a A[0][0] -= 1 var index = 1 while index * 2 <= A[0][0]: if index * 2 + 1<= A[0][0]: if A[index][key] < A[index * 2][key] and A[index * 2 + 1][key] <= A[index * 2][key]: (A[index],A[index * 2]) = (A[index * 2],A[index]) index = index * 2 elif A[index][key] < A[index * 2 + 1][key]: (A[index],A[index * 2 + 1]) = (A[index * 2 + 1],A[index]) index = index * 2 + 1 else: break elif index * 2 <= A[0][0]: if A[index][key] < A[index * 2][key]: (A[index],A[index * 2]) = (A[index * 2],A[index]) break else: break proc size(A : priorityQue):int= return A[0][0] var N,M,S,G : int a,b,c : int (N, M, S, G) = stdin.readline.split.map(parseInt) var hyou = newSeqWith(N + 1, newSeq[dist](0)) for m in 1..M: (a, b, c) = stdin.readline.split.map(parseInt) hyou[a].add((b, c)) hyou[b].add((a, c)) var pq : priorityque[400000,item] t : item mincos : array[201, seq[int]] for i in 0..200: mincos[i] = @[int.high] block tansaku: pq.push((S, @[0, S])) while pq.size > 0: t = pq.pop var c = t.cost[0] if t.id == G: echo t.cost[1..t.cost.high].join(" ") break tansaku for p in hyou[t.id]: var q = t q.cost[0] += p.d q.cost.add(p.s) if (q.cost < mincos[p.s]) == false: mincos[p.s] = q.cost q.id = p.s pq.push(q)