题面:
Colored Path
Time limit: 1.5 seconds
Memory limit: 256 mebibytes
Problem Description
You have a board of size n × n. Each cell of the board has weight and color. Both weight and color are
positive integers. Rows and columns are enumerated from 1 to n. Let (i, j) be j-th cell of i-th row. In one
step you can move from cell (i, j) to cells (i, j + 1) and (i + 1, j).
Consider all paths from (1, 1) to (n, n) that obey the rule above. Obviously each such path contains
exactly 2n − 1 cells. Let’s define the weight of the path as the sum of the weights of the cells on the path.
Let’s define the colorness of the path as the number of different colors among the colors of the cells on
the path.
Given the weights and the colors of all cells, find the smallest colorness among all paths with weight no
more than W or report that there are no such paths.
Input
The first line contains three integers: n (1 ≤ n ≤ 400), k (1 ≤ k ≤ 10) which is the number of possible
colors, and W (1 ≤ W ≤ 1 0 9 10^9 109). Each of the next n lines contains n integers, j-th integer on i-th line is the
weight of the cell (i, j) (1 ≤ w i j w_{ij} wij ≤ 1 0 6 10^6 106). The last n lines contain n integers each, j-th integer on i-th line
is the color of the cell (i, j) (1 ≤ c i j c_{ij} cij ≤ k).
Output
On the first line output the minimal colorness of the path. On the second line output the path with
the minimal colorness in the format i 1 i_1 i1 j 1 j_1 j1 i 2 i_2 i2 j 2 j_2 j2 . . . i 2 n − 1 i_{2n−1} i2n−1 j 2 n − 1 j_{2n−1}