## MKTHPATH - Kth Shortest Path

no tags

`Isaac is tired of his daily trip to his office, using the same shortest route everyday. Although this saves his time, he must see the same scenery againand again. He cannot stand such a boring commutation any more. One day, he decided to improve the situation. He would change his route everyday at least slightly. His new scheme is as follows. On the first day, he uses the shortest route. On the second day, he uses the second shortestroute, namely the shortest except one used on the first day. In general, on the k-th day, the k-th shortest route is chosen. Visiting the same place twice on a route should be avoided, of course.You are invited to help Isaac, by writing a program which finds his routeon the k-th day. The problem is easily modeled using terms in the graph theory. Your program should find the k-th shortest path in the given directed graph.`

### Input

`The input consists of multiple datasets, each in the following format.    n	m	k	a	b    x1	y1	d1	    x2	y2	d2    …    xm	ym	dmEvery input item in a dataset is a non-negative integer. Two or more inputitems in a line are separated by a space.n is the number of nodes in the graph. You can assume the inequality 2 ≤ n ≤ 50. m is the number of (directed) edges. a is the start node, and b is the goalnode. They are between 1 and n, inclusive. You are required to find the k-th shortest path from a to b. You can assume 1 ≤ k ≤ 200 and a ≠ b.The i-th edge is from the node xi to yi with the length di (1 ≤ i ≤ m). Both xi and yi are between 1 and n, inclusive. di is between 1 and 10000, inclusive. You can directly go from xi to yi, but not from yi to xi unless an edge from yi to xi is explicitly given. The edge connecting the same pair of nodes is unique, if any, that is, if i ≠ j, it is never the case that xi equals xj and yi equals yj. Edges are not connecting a node to itself, that is, xi never equals yi. Thus the inequality 0 ≤ m ≤ n(n − 1) holds.Note that the given graph may be quite unrealistic as a road network. Both the cases m = 0 and m = n(n − 1) are included in the judges’ data.The last dataset is followed by a line containing five zeros (separated by a space).SAMPLE INPUT5 20 10 1 51 2 11 3 21 4 11 5 32 1 12 3 12 4 22 5 23 1 13 2 23 4 13 5 14 1 14 2 14 3 14 5 25 1 15 2 15 3 15 4 14 6 1 1 42 4 21 3 21 2 11 4 32 3 13 4 13 3 5 1 31 2 12 3 11 3 10 0 0 0 0`

### Output

` For each dataset in the input, one line should be output as specified below.An output line should not contain extra characters such as spaces.If the number of distinct paths from a to b is less than k, the string "None" should be printed. Note that the first letter of None is in uppercase, while the other letters are in lowercase.If the number of distinct paths from a to b is k or more, the node numbers visited in the k-th shortest path should be printed in the visited order, separated by a hyphen (minus sign). Note that a must be the first, and b must be the last in the printed line.In this problem the term shorter (thus shortest also) has a special meaning. A path P is defined to be shorter than Q, if and only if one of the followingconditions holds.   1. The length of P is less than the length of Q. The length of a path is defined to be the sum of lengths of edges on the path.   2. The length of P is equal to the length of Q, and P’s sequence ofnode numbers comes earlier than Q’s in the dictionary order. Let’s specify the latter condition more precisely. Denote P’s sequence of node numbers by p1, p2, …, ps, and Q’s by q1, q2, …, qt. p1 = q1 = a and ps = qt = b shouldbe observed. The sequence P comes earlier than Q in the dictionary order, if for some r (1 ≤ r ≤ s and r ≤ t), p1 = q1, …, p(r − 1) = q(r − 1), and pr < qr (pr is numerically smaller than qr).A path visiting the same node twice or more is not allowed. SAMPLE OUTPUT1-2-4-3-51-2-3-4None`

 Added by: ~!(*(@*!@^& Date: 2009-02-23 Time limit: 0.600s-1s Source limit: 50000B Memory limit: 1536MB Cluster: Cube (Intel G860) Languages: All except: ASM64 ERL JS-RHINO NODEJS OBJC PERL6 SQLITE VB.NET Resource: Yokohama 2006