X Olympiad in Informatics 2002/2003

Problem: Connections

Author: Krzysztof Sikora

Byteotian Ministry of Infrastructure has decided to create a computer program that helps to find quickly the lengths of routes between arbitrary towns. It would be small wonder if the inhabitants of Byteotia always wanted to find the shortest route. However, it happens that they want to know the kth shortest route. Moreover, cycles in routes are possible, i.e. routes that have recurring towns.
In the first line of the standard input there are two positive integers n and m, separated by a single space, 1 <= n <= 100, 0 <= m <= n^{2}n. They are the number of towns in Byteotia and the number of roads connecting the towns, respectively. The towns are numbered from 1 to n.
In each of m successive lines there are three integers separated by single spaces: a, b and l, a <> b, 1 <= l <= 500. Each triple describes one oneway road of length l enabling to move from the town a to b. For each two towns there exist at most one road that enables to move in the given direction.
In the following line there is one integer q, 1 <= q <= 10000, denoting the number of queries. In the successive q lines there are queries written, one per line. Each query has a form of three integers separated by single spaces: c, d and k, 1 <= k <= 100. Such a query refers to the length of the kth shortest route from the town c to the town d.
5 5 1 2 3 2 3 2 3 2 1 1 3 10 1 4 1 8 1 3 1 1 3 2 1 3 3 1 4 2 2 5 1 2 2 1 2 2 2 1 1 2the correct answer is in the following output:
5 8 10 1 1 3 6 1