This page is still under construction.

Parts of this page are still being built. What you see may change.

Olympic Avenues

Interview

Time limit1sMemory limit128 MB

Summary
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 NN, the number of Olympic sites (5≤N≤505 \le N \le 50). The second line contains two integers SS and FF: the numbers of the starting and ending sites of the route. The third line contains one integer LL, the number of Olympic avenues. Each of the following LL lines contains three integers II, JJ, and DD: the two sites II and JJ connected by the avenue and the distance DD between them (DD is a positive integer). Sites are numbered from 1 to NN. 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 SS to FF. On the second line, print the numbers of the sites visited along that route in order, with SS first and FF 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 SS and FF are the same site, the length is 0 and the second line contains only SS.

Examples2

  1. Example 1

    Input
    6
    1 4
    8
    1 2 12
    1 6 8
    1 3 20
    6 5 10
    5 4 7
    5 3 2
    3 4 6
    2 3 5
    
    Expected output
    23
    1 2 3 4
    
  2. Example 2

    Input
    5
    1 5
    6
    1 2 5
    1 3 5
    1 4 5
    2 5 5
    3 5 5
    4 5 5
    
    Expected output
    10
    1 2 5