Dijkstra’s shortest paths · Compiler
Dijkstra’s shortest paths
Use Dijkstra's algorithm to find the cheapest total fare from stop s to stop t. Each ticket pays for one directed edge and route costs add.
- Input
- The first line contains n m s t. Vertices are numbered 0 through n-1 (1 <= n <= 50, 0 <= m <= 200). Each of the next m lines contains u v w for an edge from u to v. Endpoints differ and each ordered pair occurs at most once. Both s and t are vertex IDs. Each fare w is an integer from 0 to 100.
- Output
- Print the minimum total fare, or -1 if no route exists. Staying at the starting stop costs 0.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…