Homework lecture 11
Shortest Path
Given n cities (numbered from 1 to n) and m roads connecting cities. The dirty level
between two cities u, v is D[u,v] (D[u,v] might be negative). You have two tasks:
a. Write a program to find a path from a starting point s to the end point e
such that the total dirty level on the path is the smallest.
b. Write a program to find the shortest paths for all pairs of vertices
Input: Data come from file dirty.txt in the following format:
- The first line contains four integer numbers n, m, s, e
- m following lines each contains 3 integer numbers u, v, d indicating that the
dirty level of road from u to v is d.
Output: Results are written to file “dirty.out” in the following format:
- The first line contains the total dirty level of the path from s to e.
- The second line contains cities on the path from s to e.
- The next n lines each contains n integer numbers are the shortest distance
matrix for all pairs of vertices. (output INF if there is no path between two
cities).
Example:
dirty.txt dirty.out
5932 8
125 312
232 05623
434 50278
451 38056
511 24401
523 13530
357
142
313
Tips:
- Finding the minimal dirty level (called X).
- Each dirty level D[u,v]: D[u,v]=D[u,v]+ |X|. So that every dirty level is not negative.
- Apply Dijkstra algorithm to find the shortest path from s to e.
- Apply FLOYD Algorithm to find the shortest paths for every vertex pairs (u,v).