#C3451. Shortest Path in a Monorail System
Shortest Path in a Monorail System
Shortest Path in a Monorail System
You are given a monorail network consisting of n stations and m bidirectional tracks. Each track connects two different stations. Your task is to compute the shortest distance from a starting station s to an ending station t using the tracks available.
If a path exists, output the minimum number of tracks that need to be traversed. If no path exists, output -1
.
The distance between two consecutive stations is considered 1. Mathematically, if the stations along the path are represented as a sequence, then the length of the path is given by:
$$L = \sum_{i=1}^{k} 1 = k$$
where k is the number of edges in the path from s to t.
inputFormat
The input is given via standard input.
The first line contains two integers n and m ($1 \leq n \leq 10^3$, $0 \leq m \leq 10^5$), where n denotes the number of stations and m denotes the number of tracks.
The following m lines each contain two integers u and v describing a track between stations u and v.
The final line contains two integers s and t, representing the starting and ending stations respectively.
outputFormat
Output the shortest distance (an integer) from station s to station t on standard output. If there is no valid path, output -1
.
6 7
1 2
2 3
2 4
3 4
4 5
5 6
3 6
1 6
3