Another Contest 2 Problem 1 - Poutine

View as PDF

Submit solution


Points: 15
Time limit: 0.6s
Memory limit: 256M

Problem types

Fast Fingers Thomas is delivering poutine to Wilson's restaurants!

Fast Fingers Thomas will drive a truck on a directed, weighted graph with N vertices. The weight of each edge in the graph is the length of the edge. He has Q trips he needs to make.

Each trip has three parameters, a source vertex si, a destination vertex ti, and a number of days di. Thomas has di days to travel from vertex si to vertex ti. In a given day, Thomas starts at a vertex and traverses a nonnegative number of edges, ending the day at some vertex (possibly the same one). Define f(si,ti,di) to be the smallest value such that, in a single day, the sum of the weights of the edges that Thomas drives on does not exceed f(si,ti,di), and subject to this, Thomas can get from si to ti in di days. In the event that it's impossible to do this, Thomas does no driving and f(si,ti,di) is zero.

Constraints

2N100

1Q105

0wij109

1si,tiN

siti

1di<N

All queries are pairwise distinct.

Input Specification

The first line contains a single positive integer, N.

The next N lines contain N space-separated non-negative integers. The jth integer in the ith line of this section, wij, indicates the length of the directed edge connecting vertex i to vertex j, or 0 if no such edge exists. It is guaranteed there are no self-loops.

The next line contains a single positive integer, Q.

The next Q lines each contain three space-separated positive integers, si, ti, and di, representing a query for f(si,ti,di).

Output Specification

Output Q lines. On the ith line, output the answer to the ith query.

Sample Input

Copy
3
0 1 2
1 0 1
2 1 0
2
1 3 1
1 3 2

Sample Output

Copy
2
1

Comments

There are no comments at the moment.