|
| 1 | +import heapq |
| 2 | +import math |
| 3 | +import sys |
| 4 | +from collections import defaultdict |
| 5 | + |
| 6 | +read = lambda: sys.stdin.readline().rstrip() |
| 7 | + |
| 8 | + |
| 9 | +class Problem: |
| 10 | + def __init__(self): |
| 11 | + self.n = int(read()) |
| 12 | + self.m = int(read()) |
| 13 | + self.bus = defaultdict(list) |
| 14 | + |
| 15 | + for _ in range(self.m): |
| 16 | + src, dest, weight = map(int, read().split()) |
| 17 | + self.bus[src - 1].append((dest - 1, weight)) |
| 18 | + |
| 19 | + self.src, self.dest = map(lambda x: int(x) - 1, read().split()) |
| 20 | + |
| 21 | + def solve(self) -> None: |
| 22 | + weights = [math.inf] * self.n |
| 23 | + weights[self.src] = 0 |
| 24 | + |
| 25 | + queue, path = [(0.0, self.src)], [-1 for _ in range(self.n)] |
| 26 | + while queue: |
| 27 | + weight, city = heapq.heappop(queue) |
| 28 | + weight = -weight |
| 29 | + if weights[city] < weight: |
| 30 | + continue |
| 31 | + |
| 32 | + for next_city, next_weight in self.bus[city]: |
| 33 | + if weight + next_weight < weights[next_city]: |
| 34 | + weights[next_city] = weight + next_weight |
| 35 | + path[next_city] = city |
| 36 | + heapq.heappush(queue, (-weights[next_city], next_city)) |
| 37 | + |
| 38 | + res = self.find_path(path) |
| 39 | + |
| 40 | + print(int(weights[self.dest])) |
| 41 | + print(len(res)) |
| 42 | + print(*res) |
| 43 | + |
| 44 | + def find_path(self, path: list[int]) -> list[int]: |
| 45 | + current, result = self.dest, [] |
| 46 | + while current != -1: |
| 47 | + result.append(current + 1) |
| 48 | + current = path[current] |
| 49 | + |
| 50 | + return list(reversed(result)) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + Problem().solve() |
0 commit comments