Shortest path

Write a program that, given a directed graph with n vertices (numbered
from 0 to n − 1) and m arcs, prints the shortest way to go from 0 to
n − 1.

Input

Input consists of several cases. Every case begins with n and m. Follow
m pairs x y to indicate an arc from x to y. There are no repeated arcs
nor of the kind x x. There is always a path between 0 and n − 1. You can
assume 2 ≤ n ≤ 10⁴ and 1 ≤ m ≤ 5n.

Output

For every case, print the vertices of the shortest path between 0 and
n − 1 separated by spaces. If there is more than one shortest path,
print the smallest in lexicographical order.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T11:53:48.743Z

© Jutge.org, 2006–2026.
https://jutge.org
