Shortest paths with BFS · Compiler
Shortest paths with BFS
Use breadth-first search to find the fewest trails needed to walk from s to t. Every trail is two-way and takes one step.
- Input
- The first line contains n m s t. Vertices are numbered 0 through n-1 (1 <= n <= 50, 0 <= m <= 200). Each of the next m lines contains u v for an edge joining u and v in both directions. Endpoints differ and each unordered pair occurs at most once. Both s and t are vertex IDs.
- Output
- Print the minimum number of steps, or -1 if t is unreachable. Starting at the destination takes 0 steps.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…