#K68167. Shortest Path in an Undirected Weighted Graph
Shortest Path in an Undirected Weighted Graph
Shortest Path in an Undirected Weighted Graph
You are given an undirected weighted graph with \(n\) nodes and \(m\) edges. Each edge is given as a tuple \((u, v, w)\), where \(w\) is the weight of the edge connecting nodes \(u\) and \(v\).
Your task is to compute the length of the shortest path between two specified nodes \(s\) and \(t\). If there is no path from \(s\) to \(t\), output \(-1\). Note that if \(s = t\), the answer is \(0\).
You are required to use Dijkstra's algorithm to solve the problem efficiently.
inputFormat
The input is given via standard input and consists of multiple lines:
- The first line contains two space-separated integers \(n\) and \(m\) representing the number of nodes and edges in the graph.
- The following \(m\) lines each contain three integers \(u\), \(v\), and \(w\), representing an undirected edge connecting nodes \(u\) and \(v\) with weight \(w\).
- The last line contains two integers \(s\) and \(t\) denoting the start and target nodes respectively.
outputFormat
Output a single integer which is the length of the shortest path from node \(s\) to node \(t\). If no such path exists, output \(-1\).
## sample2 1
1 2 4
1 2
4