#K84462. Shortest Network Latency
Shortest Network Latency
Shortest Network Latency
You are given a network of n servers, connected by m cables. Each cable connects two servers and has an associated latency. The network can be modeled as an undirected weighted graph.
Your task is to determine the minimum total latency required to send a message from a given start server s to a target server t using Dijkstra's algorithm. If the target server is not reachable from the start server, output -1
. Note that if the start and target servers are the same, the answer is 0
.
The latency between two servers along a path is the sum of the latencies of the cables used along the path.
inputFormat
The input is given via stdin and has the following format:
- The first line contains two integers n and m — the number of servers and cables, respectively.
- The following m lines each contain three integers u, v, and w, representing a cable between server u and server v with latency w.
- The last line contains two integers s and t — the start server and the target server.
outputFormat
Output a single integer to stdout: the minimum total latency from server s to server t. If there is no path between s and t, output -1
.
5 6
1 2 7
1 3 9
1 5 14
2 3 10
2 4 15
3 4 11
1 4
20