#C7478. Minimal Distance in a Directed Graph
Minimal Distance in a Directed Graph
Minimal Distance in a Directed Graph
You are given a directed graph representing a network of servers. There are N servers and M directed edges. Each edge from server u to server v has an associated positive weight d. Your task is to compute the minimal distance from a given source server to a destination server.
The minimal distance is defined as the sum of the weights along the chosen path:
If the destination is unreachable from the source, output -1
.
Note: Use Dijkstra's algorithm to solve this problem.
inputFormat
The input is read from stdin and has the following format:
- The first line contains two integers N and M, representing the number of servers and the number of directed edges respectively.
- The next M lines each contain three integers u, v and d indicating there is a directed edge from server u to server v with weight d.
- The last line contains two integers, the source and destination servers.
outputFormat
Output the minimal distance from the source server to the destination server on a single line to stdout. If the destination is unreachable, output -1
.
5 6
1 2 2
1 3 4
2 3 1
3 4 7
2 4 3
4 5 1
1 5
6