import heapq def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]); idx +=1 M = int(input[idx]); idx +=1 S = int(input[idx]); idx +=1 G = int(input[idx]); idx +=1 adj = [[] for _ in range(N)] for _ in range(M): a = int(input[idx]); idx +=1 b = int(input[idx]); idx +=1 c = int(input[idx]); idx +=1 adj[a].append( (b, c) ) adj[b].append( (a, c) ) INF = float('inf') distances = [INF] * N distances[S] = 0 paths = [None] * N paths[S] = [S] heap = [] heapq.heappush(heap, (0, [S], S)) found = False while heap: current_dist, current_path, current_node = heapq.heappop(heap) if current_node == G: print(' '.join(map(str, current_path))) found = True break if current_dist > distances[current_node]: continue if current_path != paths[current_node]: continue for neighbor, cost in adj[current_node]: new_dist = current_dist + cost new_path = current_path + [neighbor] if new_dist < distances[neighbor]: distances[neighbor] = new_dist paths[neighbor] = new_path.copy() heapq.heappush(heap, (new_dist, new_path, neighbor)) elif new_dist == distances[neighbor]: if paths[neighbor] is None or new_path < paths[neighbor]: paths[neighbor] = new_path.copy() heapq.heappush(heap, (new_dist, new_path, neighbor)) if not found: pass # According to the problem statement, a path always exists. if __name__ == "__main__": main()