Olympic Avenues
InterviewTime limit1sMemory limit128 MB
Given an undirected weighted graph with N up to 50, find the shortest path from S to F and, among ties, print the lexicographically smallest site sequence.
- Level
Medium5 of 10
- Topics
- Graph, Shortest path, Greedy, Implementation
- Solved
- No attempts yet
Problem
At the Athens 2004 Olympic Games there were many Olympic sites (stadiums, the Olympic Village, the Press Center, offices, and so on). So that athletes, officials, and journalists could move quickly between the sites, Athens built roads called "Olympic Avenues" on which only Olympic buses were allowed. Each Olympic avenue directly connects exactly two Olympic sites. Not every pair of sites is directly connected, and a single avenue joins exactly two sites. A bus driver wants to travel from one Olympic site to another using only Olympic avenues. Given the information about the sites and the avenues, find a shortest route between two sites that uses only Olympic avenues. An avenue can be traveled in either direction.
Input
The first line contains one integer , the number of Olympic sites (). The second line contains two integers and : the numbers of the starting and ending sites of the route. The third line contains one integer , the number of Olympic avenues. Each of the following lines contains three integers , , and : the two sites and connected by the avenue and the distance between them ( is a positive integer). Sites are numbered from 1 to . The input has the following form.
N
S F
L
I1 J1 D1
I2 J2 D2
...
IL JL DL
Output
On the first line, print the length (total distance) of the shortest route from to . On the second line, print the numbers of the sites visited along that route in order, with first and last. If several shortest routes exist, print the one whose sequence of site numbers is lexicographically smallest (compare the two sequences element by element from the front; the sequence with the smaller element comes first). If and are the same site, the length is 0 and the second line contains only .