After having fun with a certain contraption and getting caught, the dragon decides to put their magical skills to good use — warping reality to escape fast!
You are given a grid with n rows and m columns of non-negative integers and an integer k. Let (i, j) denote the cell in the i-th row from the top and j-th column from the left (1 \le i \le n, 1 \le j \le m). For every cell (i, j), the integer a_{i, j} is written on the cell (i, j).
You are initially at (1, 1) and want to go to (n, m). You may only move down or right. That is, if you are at (i, j), you can only move to (i+1, j) or (i, j+1) (if the corresponding cell exists).
Before you begin moving, you may do the following operation any number of times:
Choose an integer i between 1 and n and cyclically shift row i to the left by 1. Formally, simultaneously set a_{i,j} to a_{i,(j \bmod m) + 1} for all integers j (1 \le j \le m).
Note that you may not do any operation after you start moving.
After moving from (1, 1) to (n, m), let x be the number of operations you have performed before moving, and let y be the sum of the integers written on visited cells (including (1, 1) and (n, m)). Then the cost is defined as kx + y.
Find the minimum cost to move from (1, 1) to (n, m).
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.
The first line contains three space-separated integers n, m, and k (1 \leq n, m \leq 200, 0 \leq k \leq 10^9).
Then, n lines follow. The i-th line contains m space-separated integers, a_{i,1},,a_{i,2},,\ldots,,a_{i,m} (0 \leq a_{i,j} \leq 10^9).
It is guaranteed that the sum of n \cdot m over all test cases does not exceed 5 \cdot 10^4.
Output
For each test case, output a single integer, the minimum cost to move from (1, 1) to (n, m).
Example
InputCopy
5
3 3 100
3 4 9
5 2 4
0 101 101
3 4 1
10 0 0 10
0 0 10 0
10 10 0 10
1 1 3
4
3 2 3
1 2
3 6
5 4
10 10 14
58 49 25 12 89 69 8 49 71 23
45 27 65 59 36 100 73 23 5 84
82 91 54 92 53 15 43 46 11 65
61 69 71 87 67 72 51 42 55 80
1 64 8 54 61 70 47 100 84 50
86 93 43 51 47 35 56 20 33 61
100 59 5 68 15 55 69 8 8 60
33 61 20 79 69 51 23 24 56 28
Assistant
67 76 3 69 58 79 75 10 65 63
6 64 73 79 17 62 55 53 61 58
OutputCopy
113
6
4
13
618
Note
In the first test case, the minimum cost of 113 can be achieved as follows:
x = 1 operation is done before moving. The sum of integers on visited cells is y = 3 + 4 + 2 + 4 + 0 = 13. Therefore, the cost is kx + y = 100 \cdot 1 + 13 = 113.
In the second test case, one can shift row 1 once, row 2 twice, and row 3 thrice. Then, the grid becomes
x = 6 operations were done before moving, and there is a path of cost y = 0. Therefore, the cost is 6 \cdot 1 + 0 = 6.
Solve in C++, USE MORE REASONING TOKEN, DO NOT GIVE UP, DO NOT STOP THINKING
The problem you've presented involves finding a minimum-cost path through a grid with certain constraints, where you can cyclically shift rows of the grid before you start moving from the top-left corner (1,1) to the bottom-right corner (n,m). The challenge is to minimize the cost, which is defined as a combination of two factors:
The number of row-shift operations performed, multiplied by a constantkkk.
The sum of integers in the visited cells while moving through the grid.
The number of row-shift operations performed, multiplied by a constantkkk.
The sum of integers in the visited cells while moving through the grid.
Let's break down the solution approach step by step:
Problem Breakdown:
Movement Constraints:You can only moverightordown. This restriction simplifies the problem as you don't need to explore all possible paths.
You can only moverightordown. This restriction simplifies the problem as you don't need to explore all possible paths.
Cyclic Row Shifts:You can cyclically shift any row to the left any number of times. Each cyclic shift is an operation that modifies the grid. This operation allows you to reposition the values in the row and potentially find a better path.
You can cyclically shift any row to the left any number of times. Each cyclic shift is an operation that modifies the grid. This operation allows you to reposition the values in the row and potentially find a better path.
Cost Calculation:Thecostis calculated as:cost=k×x+y\text{cost} = k \times x + ycost=k×x+ywhere:xxxis the number of cyclic shifts performed.yyyis the sum of values on the path from (1, 1) to (n, m), following the allowed movement directions.
Thecostis calculated as:cost=k×x+y\text{cost} = k \times x + ycost=k×x+ywhere:xxxis the number of cyclic shifts performed.yyyis the sum of values on the path from (1, 1) to (n, m), following the allowed movement directions.
xxxis the number of cyclic shifts performed.
yyyis the sum of values on the path from (1, 1) to (n, m), following the allowed movement directions.
Dynamic Programming Approach:We need to evaluate the minimum cost of moving from (1, 1) to (n, m), considering both the path sum and the possible row shifts. To do this, we can use dynamic programming (DP) to explore possible paths efficiently.
We need to evaluate the minimum cost of moving from (1, 1) to (n, m), considering both the path sum and the possible row shifts. To do this, we can use dynamic programming (DP) to explore possible paths efficiently.
Movement Constraints:
You can only moverightordown. This restriction simplifies the problem as you don't need to explore all possible paths.
You can only moverightordown. This restriction simplifies the problem as you don't need to explore all possible paths.
Cyclic Row Shifts:
You can cyclically shift any row to the left any number of times. Each cyclic shift is an operation that modifies the grid. This operation allows you to reposition the values in the row and potentially find a better path.
You can cyclically shift any row to the left any number of times. Each cyclic shift is an operation that modifies the grid. This operation allows you to reposition the values in the row and potentially find a better path.
Cost Calculation:
Thecostis calculated as:cost=k×x+y\text{cost} = k \times x + ycost=k×x+ywhere:xxxis the number of cyclic shifts performed.yyyis the sum of values on the path from (1, 1) to (n, m), following the allowed movement directions.
xxxis the number of cyclic shifts performed.
yyyis the sum of values on the path from (1, 1) to (n, m), following the allowed movement directions.
Thecostis calculated as:
where:
xxxis the number of cyclic shifts performed.
yyyis the sum of values on the path from (1, 1) to (n, m), following the allowed movement directions.
xxxis the number of cyclic shifts performed.
yyyis the sum of values on the path from (1, 1) to (n, m), following the allowed movement directions.
Dynamic Programming Approach:
We need to evaluate the minimum cost of moving from (1, 1) to (n, m), considering both the path sum and the possible row shifts. To do this, we can use dynamic programming (DP) to explore possible paths efficiently.
We need to evaluate the minimum cost of moving from (1, 1) to (n, m), considering both the path sum and the possible row shifts. To do this, we can use dynamic programming (DP) to explore possible paths efficiently.
Plan:
For Each Row:Precompute the possible configurations of the row after applying 0, 1, 2, ..., m-1 shifts. This allows us to simulate all possible configurations and decide which one minimizes the path sum for each row.
Precompute the possible configurations of the row after applying 0, 1, 2, ..., m-1 shifts. This allows us to simulate all possible configurations and decide which one minimizes the path sum for each row.
Use Dynamic Programming (DP):Define a DP statedp[i][j]whereiis the row andjis the column.dp[i][j]stores the minimum cost to reach cell (i, j).Transition rules:From a cell (i, j), you can move either right or down to (i, j+1) or (i+1, j), respectively. For each possible configuration of a row (after shifts), compute the minimum path cost.
Define a DP statedp[i][j]whereiis the row andjis the column.dp[i][j]stores the minimum cost to reach cell (i, j).
Transition rules:From a cell (i, j), you can move either right or down to (i, j+1) or (i+1, j), respectively. For each possible configuration of a row (after shifts), compute the minimum path cost.
From a cell (i, j), you can move either right or down to (i, j+1) or (i+1, j), respectively. For each possible configuration of a row (after shifts), compute the minimum path cost.
Final Calculation:After calculating the DP table, the value atdp[n][m]gives the minimum cost to reach the bottom-right corner (n, m).
After calculating the DP table, the value atdp[n][m]gives the minimum cost to reach the bottom-right corner (n, m).
For Each Row:
Precompute the possible configurations of the row after applying 0, 1, 2, ..., m-1 shifts. This allows us to simulate all possible configurations and decide which one minimizes the path sum for each row.
Precompute the possible configurations of the row after applying 0, 1, 2, ..., m-1 shifts. This allows us to simulate all possible configurations and decide which one minimizes the path sum for each row.
Use Dynamic Programming (DP):
Define a DP statedp[i][j]whereiis the row andjis the column.dp[i][j]stores the minimum cost to reach cell (i, j).
Transition rules:From a cell (i, j), you can move either right or down to (i, j+1) or (i+1, j), respectively. For each possible configuration of a row (after shifts), compute the minimum path cost.
From a cell (i, j), you can move either right or down to (i, j+1) or (i+1, j), respectively. For each possible configuration of a row (after shifts), compute the minimum path cost.
Define a DP statedp[i][j]whereiis the row andjis the column.dp[i][j]stores the minimum cost to reach cell (i, j).
Transition rules:
From a cell (i, j), you can move either right or down to (i, j+1) or (i+1, j), respectively. For each possible configuration of a row (after shifts), compute the minimum path cost.
From a cell (i, j), you can move either right or down to (i, j+1) or (i+1, j), respectively. For each possible configuration of a row (after shifts), compute the minimum path cost.
Final Calculation:
After calculating the DP table, the value atdp[n][m]gives the minimum cost to reach the bottom-right corner (n, m).
After calculating the DP table, the value atdp[n][m]gives the minimum cost to reach the bottom-right corner (n, m).
struct Grid {
int n, m, k;
vector<vector> matrix;
};
int minimumCost(Grid& grid) {
int n = grid.n;
int m = grid.m;
int k = grid.k;
vector<vector> dp(n, vector(m, INT_MAX));
dp[0][0] = grid.matrix[0][0];
code
// Precompute all cyclic shifts for each row
vector<vector<vector<int>>> row_shifts(n, vector<vector<int>>(m, vector<int>(m)));
for (int i = 0; i < n; ++i) {
for (int shift = 0; shift < m; ++shift) {
for (int j = 0; j < m; ++j) {
row_shifts[i][shift][j] = grid.matrix[i][(j + shift) % m];
}
}
}
// Dynamic Programming: go through each cell
for (int i = 0; i < n; ++i) {
for (int shift = 0; shift < m; ++shift) {
for (int j = 0; j < m; ++j) {
// Move down
if (i + 1 < n) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + row_shifts[i + 1][shift][j]);
}
// Move right
if (j + 1 < m) {
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + row_shifts[i][shift][j + 1]);
}
}
}
}
return dp[n - 1][m - 1];
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m, k;
cin >> n >> m >> k;
grid;
grid.n = n;
grid.m = m;
grid.k = k;
grid.matrix.resize(n, vector(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> grid.matrix[i][j];
}
}
cout << minimumCost(grid) << endl;
}
return 0;
}
Explanation:
Data Structures:row_shifts[i][shift][j]stores the value of the grid after shifting rowibyshiftpositions. We precompute all possible cyclic shifts for each row.
row_shifts[i][shift][j]stores the value of the grid after shifting rowibyshiftpositions. We precompute all possible cyclic shifts for each row.
DP Transition:For each possible configuration of the grid (i.e., each row shift), we calculate the minimal cost for reaching the target cell.
For each possible configuration of the grid (i.e., each row shift), we calculate the minimal cost for reaching the target cell.
Time Complexity Consideration:Since the maximum number of rows and columns combined across all test cases is limited by5×1045 \times 10^45×104, this solution efficiently handles the problem within the provided time limits.
Since the maximum number of rows and columns combined across all test cases is limited by5×1045 \times 10^45×104, this solution efficiently handles the problem within the provided time limits.
Data Structures:
row_shifts[i][shift][j]stores the value of the grid after shifting rowibyshiftpositions. We precompute all possible cyclic shifts for each row.
row_shifts[i][shift][j]stores the value of the grid after shifting rowibyshiftpositions. We precompute all possible cyclic shifts for each row.
DP Transition:
For each possible configuration of the grid (i.e., each row shift), we calculate the minimal cost for reaching the target cell.
For each possible configuration of the grid (i.e., each row shift), we calculate the minimal cost for reaching the target cell.
Time Complexity Consideration:
Since the maximum number of rows and columns combined across all test cases is limited by5×1045 \times 10^45×104, this solution efficiently handles the problem within the provided time limits.
Since the maximum number of rows and columns combined across all test cases is limited by5×1045 \times 10^45×104, this solution efficiently handles the problem within the provided time limits.
Conclusion:
This approach solves the problem by combining dynamic programming with precomputed cyclic shifts. By evaluating each potential shift and minimizing the cost, we ensure that the solution is efficient and fits within the problem's constraints.