0% found this document useful (0 votes)
20 views

Problem C: Second Shortest Path: Input

Max needs help finding second shortest paths in graphs to avoid detection by agencies. The input describes a graph with vertices and edge lengths. For each start-end vertex pair, the program should output a second shortest path between them or indicate there is none.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Problem C: Second Shortest Path: Input

Max needs help finding second shortest paths in graphs to avoid detection by agencies. The input describes a graph with vertices and edge lengths. For each start-end vertex pair, the program should output a second shortest path between them or indicate there is none.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Problem C: Second Shortest Path

A number of agencies are after Max. Max is known to travel various graphs. His practice was to always choose a shortest path from a start vertex to his end vertex but he is suspects that the agencies are aware of this and are watching shortest paths. So Max has decided to use second shortest paths. Write a program to help Max find second shortest paths. In this problem only simple paths are considered, that is, paths which visit a vertex at most once and do not contains "cycles". A shortest path P is a sequence of edges whose total length is less than or equal to the total length of any other path from the start to the end. A second shortest path Q has length less than or equal to all other paths except P. The length of Q might be equal to that of P in which case it is more likely that Max will be caught. Input The standard input will contain a positive integer n and then an n by n array of nonnegative integer values. The i,j entry is the length of an edge from vertex i to vertex j or 0 if there is no such edge. Then there will be one or more pairs S,E of start,end verticies. All verticies will be numbers in the range 0,1,...,n-1. Output Your program is to print a second shortest path from S to E on one line for each such pair. There may not be two different paths (or even one) from S to E in which case the output should be as shown in C.out. If there is more than one shortest path any one of them will be accepted as second shortest. Sample Input (6 lines available as file C.in) 4 0 0 0 0 0 2 0 0 2 3 5 2 0 0 0 2 1 0 1 2

3 0

3 3

Diagram for sample input: 2 0 ---> \ \ \5 \ \ 2 1 <--->3 | / | / |2 /1 | / v / 2

Sample Output (4 lines available as file C.out) SS SS SS SS 0 1 3 3 to to to to 3: 2: 0: 3: 0 1 2 3 none none none

Note that the second shortest path from s to f in graph G is the shortest over all paths from s to f over all graphs which are the same as G but are missing one of the edges from the shortest path s to f in G.

You might also like