#P1339. Shortest Path in an Undirected Graph
Shortest Path in an Undirected Graph
Shortest Path in an Undirected Graph
You are given an undirected graph with \(n\) nodes and \(m\) edges. Your task is to compute the length of the shortest path from node \(s\) to node \(t\). The graph is unweighted, and if there is no path between \(s\) and \(t\), output -1.
Note: The nodes are numbered from 1 to \(n\).
inputFormat
The input consists of multiple lines:
- The first line contains two integers \(n\) and \(m\) representing the number of nodes and edges respectively.
- The next \(m\) lines each contain two integers \(u\) and \(v\), representing an undirected edge between nodes \(u\) and \(v\).
- The last line contains two integers \(s\) and \(t\) representing the starting and target nodes.
outputFormat
Output a single integer representing the length of the shortest path from node \(s\) to node \(t\). If there is no such path, output -1.
sample
4 4
1 2
2 3
3 4
1 4
1 3
2