#P1111B. 哈!昨日五次重现(困难版)
哈!昨日五次重现(困难版)
M. Ha, It's Yesterday Five Times More(Hard Version)
The only difference between the easy version and the hard version is that, in the hard version, the range of T, n, m is larger. X and Y are not necessarily 1, and c is not necessarily 0 or 1000.
Background
In 2018, 2020, 2021, 2022, and 2023, the SUA problem-setting team proposed five problems related to kangaroos in the ICPC Nanjing Site.
To warm up before the Jinan Site, pzr decided to solve the following problem about kangaroos. Can you help him?
Description
Given an grid, initially, each cell can have at most one kangaroo (but during the operation, a cell may contain multiple kangaroos). In addition, there's a hole in a specific cell .
You can perform several operations, each of which is one of the following:
- Cost , causing all kangaroos to gather with the hole as the center, specifically:
- All kangaroos in the cells to the left of the hole (in the same row but with column number less than the column number of the hole) move one cell to the right.
- All kangaroos in the cells to the right of the hole (in the same row but with column number greater than the column number of the hole) move one cell to the left.
- All kangaroos in the cells above the hole (in the same column but with row number less than the row number of the hole) move one cell down.
- All kangaroos in the cells below the hole (in the same column but with row number greater than the row number of the hole) move one cell up.
- All kangaroos in the cells to the top-left of the hole (with row number less than the row number of the hole and column number less than the column number of the hole) move one cell to the right and then one cell down.
- All kangaroos in the cells to the bottom-left of the hole (with row number greater than the row number of the hole and column number less than the column number of the hole) move one cell to the right and then one cell up.
- All kangaroos in the cells to the top-right of the hole (with row number less than the row number of the hole and column number greater than the column number of the hole) move one cell to the left and then one cell down.
- All kangaroos in the cells to the bottom-right of the hole (with row number greater than the row number of the hole and column number greater than the column number of the hole) move one cell to the left and then one cell up.
- Cost , choosing a specific kangaroo.
- Move it to one of the four adjacent cells (up, down, left, right).
When a kangaroo falls into the hole, it will be removed from the grid. If after a series of operations, exactly one kangaroo remains in the grid, it becomes the final winner.
Now, assuming you are a kangaroo at position , please calculate: What is the minimum cost to become the final winner? For each kangaroo, find the answers . To avoid excessive output, you only need to find the sum of the squares of the individual answers, i.e., .
Input Format
The first line contains an integer , representing the number of test cases.
For each test case:
The first line contains five integers , representing the number of rows and columns of the grid, the cost of the first operation, and the position of the hole.
Next, there are lines, each containing a string of length , consisting of only or , indicating the initial number of kangaroos in that cell. It's guaranteed that there are no kangaroos at the hole's initial position. It's guaranteed that there is at least one kangaroo in the entire grid.
Output Format
For each test case: Only one non-negative integer, representing the sum of the squares of the minimum costs for each kangaroo.
Sample Input 1
6
3 4 2 2 3
0100
1100
0110
3 4 2 2 3
1000
1101
0100
3 4 1 2 3
0100
1100
0100
3 4 100 2 3
1111
1101
1111
3 4 0 2 3
1111
1101
1111
7 10 5 3 6
1111110110
1010110010
0110101100
1100100110
1110010010
1010100100
0100110110
Sample Output 1
77
132
28
3642
90
26676
Sample 1 Explanation
For the first test case
The hole is located at (2,3).
0100
11x0
0110
For the kangaroo at (1,2), the operations are as follows:
- Move itself one cell to the left
1000
1100
0110
- All kangaroos gather towards the center
0000
0200
0000
Note that at this point, the kangaroo at (1,2) and the kangaroo at (1,1) will both be moved to (2,2).
- Move another kangaroo one cell to the right
0000
0100
0000
Therefore, the minimum cost is .
Similarly, we can obtain the minimum cost for the kangaroo at (2,1) as , at (2,2) as , at (3,2) as , and at (3,3) as .
Hence, the output is
Data Range and Constraints
Subtask 1 (10 points):
There is only one kangaroo in the entire grid.
Subtask 2 (20 points):
Subtask 3 (20 points):
It's guaranteed that the sum of in all test cases does not exceed
Subtask 4 (20 points):
Kangaroos are only distributed to the left, right, above, or below the hole.
Subtask 5 (20 points):
Subtask 6 (10 points):
No special properties.
For all test cases:
, .
, ,
It's guaranteed that there are no kangaroos initially at the hole's position. It's guaranteed that there is at least one kangaroo in the entire grid.
It's guaranteed that the sum of in all test cases does not exceed .
It can be proved that under the current constraints, any kangaroo can become the final winner.
It can be proved that under the current constraints, the answer must be representable by a -bit integer.
The English Version of the Statement is Translated By ChatGPT.
相关
在下列比赛中: