## Algorithm

D. AlgoRace
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

PMP is getting a warrior. He is practicing a lot, but the results are not acceptable yet. This time instead of programming contests, he decided to compete in a car racing to increase the spirit of victory. He decides to choose a competition that also exhibits algorithmic features.

AlgoRace is a special league of car racing where different teams compete in a country of n cities. Cities are numbered 1 through n. Every two distinct cities in the country are connected with one bidirectional road. Each competing team should introduce one driver and a set of cars.

The competition is held in r rounds. In i-th round, drivers will start at city si and finish at city ti. Drivers are allowed to change their cars at most ki times. Changing cars can take place in any city in no time. One car can be used multiple times in one round, but total number of changes should not exceed ki. Drivers can freely choose their path to destination.

PMP has prepared m type of purpose-built cars. Beside for PMP’s driving skills, depending on properties of the car and the road, a car traverses each road in each direction in different times.

PMP Warriors wants to devise best strategies of choosing car and roads in each round to maximize the chance of winning the cup. For each round they want to find the minimum time required to finish it.

Input

The first line contains three space-separated integers n, m, r (2 ≤ n ≤ 60, 1 ≤ m ≤ 60, 1 ≤ r ≤ 105) — the number of cities, the number of different types of cars and the number of rounds in the competition, correspondingly.

Next m sets of n × n matrices of integers between 0 to 106 (inclusive) will follow — describing the time one car requires to traverse different roads. The k-th integer in j-th line of the i-th set is the time that i-th car requires to traverse the road from j-th city to k-th city. These matrices are not necessarily symmetric, but their diagonal is always zero.

Next r lines contain description of the rounds. The i-th of these lines contains space-separated integers si, ti, ki (1 ≤ si, ti ≤ n, si ≠ ti, 0 ≤ ki ≤ 1000) — the number of starting city, finishing city and the number of possible car changes in i-th round, correspondingly.

Output

For each round you should print the minimum required time to complete the round in a single line.

Examples
input
Copy
`4 2 30 1 5 62 0 3 61 3 0 16 6 7 00 3 5 62 0 1 61 3 0 26 6 7 01 4 21 4 11 4 3`
output
Copy
`343`
input
Copy
`4 2 30 7 3 38 0 10 51 1 0 48 9 2 00 3 3 97 0 4 93 8 0 44 8 9 02 3 32 1 31 2 2`
output
Copy
`453`
Note

In the first sample, in all rounds PMP goes from city #1 to city #2, then city #3 and finally city #4. But the sequences of types of the cars he uses are (1, 2, 1) in the first round and (1, 2, 2) in the second round. In the third round, although he can change his car three times, he uses the same strategy as the first round which only needs two car changes.

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````#include <bits/stdc++.h>

using namespace std;

int const N = 61;
int n, m, r, cars[N][N][N], floyd[N][N][N];

int main() {
scanf("%d %d %d", &n, &m, &r);

for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
for(int k = 0; k < n; ++k)
cars[i][j][k] = 1e9;

for(int c = 0; c < m; ++c) {
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
scanf("%d", &cars[i][j][c]);

for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
cars[i][j][c] = min(cars[i][j][c], cars[i][k][c] + cars[k][j][c]);
}

for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
for(int c = 0; c < N; ++c)
floyd[i][j][c] = 1e9;

for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
for(int c = 0; c < m; ++c)
floyd[i][j][0] = min(floyd[i][j][0], cars[i][j][c]);

for(int c = 1; c <= 60; ++c)
for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
floyd[i][j][c] = min(floyd[i][j][c], floyd[i][k][c - 1] + floyd[k][j][0]);

for(int i = 0, s, t, k; i < r; ++i) {
scanf("%d %d %d", &s, &t, &k);
--s, --t, k = min(k, 60);
printf("%d\n", floyd[s][t][k]);
}

return 0;
}``````
Copy The Code &

Input

cmd
4 2 3
0 1 5 6
2 0 3 6
1 3 0 1
6 6 7 0
0 3 5 6
2 0 1 6
1 3 0 2
6 6 7 0
1 4 2
1 4 1
1 4 3

Output

cmd
3
4
3