#C5697. Shortest Path in a Weighted Graph
Shortest Path in a Weighted Graph
Shortest Path in a Weighted Graph
This problem involves computing the shortest path in a weighted undirected graph. You are given \(n\) intersections (numbered from 1 to \(n\)) and \(m\) streets. Each street connects two intersections and has a positive length (weight). Your task is to determine the shortest distance from a given source intersection \(s\) to a target intersection \(t\) using Dijkstra's algorithm. If no path exists, output -1.
The input is provided via standard input (stdin) and the answer should be printed to standard output (stdout).
inputFormat
The input consists of several lines:
- The first line contains two integers \(n\) and \(m\) -- the number of intersections and streets.
- The next \(m\) lines each contain three integers \(u\), \(v\), and \(w\), indicating there is a street between intersections \(u\) and \(v\) with weight \(w\).
- The last line contains two integers \(s\) and \(t\), the source and target intersections, respectively.
outputFormat
Output a single integer, the length of the shortest path from \(s\) to \(t\). If there is no such path, output -1.
## sample5 6
1 2 2
1 3 4
2 3 1
2 4 7
3 5 3
4 5 2
1 5
6
</p>