With the ambition of colonial expansion, Pear and his Star Fleet boarded a plain on Planet X. To assess the potential value of the land, Pear divides it into M*N lattices, each of which is represented by an integer (positive or negative).
Pear's job is simple - to choose some grids, occupy these lands, and separate them from other lands by building fences. For M*N lattices, there are (M+1)N+M(N+1) fences, i.e. each lattice has four fences, up and down, left and right; fences not on the boundary are shared by two adjacent lattices. Probably as shown in the following figure [p1.png].
In the picture, the blue section is a fence, belonging to lattices 1 and 2; the red section is a fence, belonging to lattices 3 and 4.
Each lattice has a positive and negative return, and the cost of fencing must be positive.
You need to select some grids, and then choose some fences to enclose them, so that all the selected grids and all the unselected grids are strictly separated. The selected lattices can be disconnected or have "holes", i.e. some lattices are not selected in the middle of a connected block. Note that if there is a "hole" in the middle, then by definition, the "hole" and the connecting block must also be separated.
Pear's goal is clear, with the smallest cost and the greatest benefit.
[Input data] Enter two positive integers M N in the first row to represent the number of rows and columns. Next, M rows, N integers per row, form A, A[i,j] to indicate the value of the j column lattice in line I. Next, M+1 rows, N integers per row, constitute a matrix B, B[i,j] representing the fence establishment cost above column J in row I. In particular, B[M+1,j] represents the cost of fencing under column J of row M. Next, M rows, N+1 integer per row, constitute the matrix C, C[i,j] which represents the cost of fence establishment on the left of column J in row I. In particular, C[i,N+1] represents the cost of fencing on the right side of column N in row I. [Output data] A line. There is only one positive integer, which represents the maximum return. [Input Sample 1] 3 3 65 -6 -11 15 65 32 -8 5 66 4 1 6 7 3 11 23 21 22 5 25 22 26 1 1 13 16 3 3 4 6 3 1 2 Procedures should output: 123 [Input Sample 2] 6 6 72 2 -7 1 43 -12 74 74 -14 35 5 3 31 71 -12 70 38 66 40 -6 8 52 3 78 50 11 62 20 -6 61 76 55 67 28 -19 68 25 4 5 8 30 5 9 20 29 20 6 18 3 19 20 11 5 15 10 3 19 23 6 24 27 8 16 10 5 22 28 14 1 5 1 24 2 13 15 17 23 28 24 11 27 16 12 13 27 19 15 21 6 21 11 5 2 3 1 11 10 20 9 8 28 1 21 9 5 7 16 20 26 2 22 5 12 30 27 16 26 9 6 23 Procedures should be output 870
[Data Scope]
For 20% of the data, M, N<=4
For 50% of the data, M, N<=15
For 100% of the data, M, N<=200
The absolute values of arrays A, B and C (all the grid and fence input data involved) are not more than 1000. According to the meaning of the question, A arrays can be positive or negative, and B and C arrays are positive integers.
Resource agreements:
Peak memory consumption < 256M
CPU consumption < 3000ms
Please output strictly according to the requirements, and do not print something like "Please input..." Excess content.
All the code is placed in the same source file. After debugging is passed, the source code is copied and submitted.
Note: The main function needs to return 0
Note: Use only the ANSI C/ANSI C++ standard and do not call special functions that depend on the compiler environment or operating system.
Note: All dependent functions must be explicitly # include d in the source file, and common header files cannot be omitted through engineering settings.
When submitting, pay attention to selecting the desired compiler type.
Please forgive the blogger for being a little white. The following code can only pass some test cases. I hope the blogger can comment on it in time.import java.util.Scanner; public class Main { public static int m, n; public static int[][] A; public static int[][] B; public static int[][] C; public int[][] step = {{0,1},{1,0}}; //Tables represent one step to the right and one step down in M*N cells public int[][] step1 = {{-1,0},{1,0},{0,-1},{0,1}};//Step up, step down, step left and step right public void init() { A = new int[m][n]; B = new int[m + 1][n]; C = new int[m][n + 1]; } public void getResult() { int[][] judge = new int[n][m]; for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) if(A[i][j] < 0) //Revenue is negative and discarded directly judge[i][j] = -1; for(int i = 0;i < m;i++) for(int j = 0;j < n;j++) { int v = B[i][j] + B[i + 1][j] + C[i][j] + C[i][j + 1]; A[i][j] = A[i][j] - v; if(A[i][j] >= 0) //Subtract the cost of fencing, the income is not negative, it must be included. judge[i][j] = 1; } for(int i = 0;i < m;i++) //Dealing with Repeated Problems of Adjacent Fences for(int j = 0;j < n;j++) { if(judge[i][j] == -1 || judge[i][j] == 0) continue; for(int k = 0;k < 2;k++) { int x = i + step[k][0]; int y = j + step[k][1]; if(x < m && y < n) { if(judge[x][y] == 1) { if(k == 0) { A[i][j] = A[i][j] + C[x][y]; A[x][y] = A[x][y] + C[x][y]; } else { A[i][j] = A[i][j] + B[x][y]; A[x][y] = A[x][y] + B[x][y]; } } } } } //Scan again to select cells that may meet the requirements for(int i = 0;i < m;i++) for(int j = 0;j < n;j++) { if(judge[i][j] != 1) { for(int k = 0;k < 4;k++) { int x = i + step1[k][0]; int y = j + step1[k][1]; if(x < m && y < n && x >= 0 && y >= 0 && judge[x][y] == 1) { if(k == 0) { A[i][j] = A[i][j] + 2 * B[x + 1][y]; } else if(k == 1){ A[i][j] = A[i][j] + 2 * B[x][y]; } else if(k == 2) { A[i][j] = A[i][j] + 2 * C[x][y + 1]; } else { A[i][j] = A[i][j] + 2 * C[x][y]; } } } if(A[i][j] >= 0) judge[i][j] = 1; else { for(int k = 0;k < 4;k++) { int x = i + step1[k][0]; int y = j + step1[k][1]; if(x < m && y < n && x >= 0 && y >= 0 && judge[x][y] == 1) { if(k == 0) { A[i][j] = A[i][j] - 2 * B[x + 1][y]; } else if(k == 1){ A[i][j] = A[i][j] - 2 * B[x][y]; } else if(k == 2) { A[i][j] = A[i][j] - 2 * C[x][y + 1]; } else { A[i][j] = A[i][j] - 2 * C[x][y]; } } } } } } int sum = 0; for(int i = 0;i < m;i++) for(int j = 0;j < n;j++) if(A[i][j] >= 0) sum = sum + A[i][j]; System.out.println(sum); } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); m = in.nextInt(); n = in.nextInt(); test.init(); for(int i = 0;i < m;i++) for(int j = 0;j < n;j++) A[i][j] = in.nextInt(); for(int i = 0;i < m + 1;i++) for(int j = 0;j < n;j++) B[i][j] = in.nextInt(); for(int i = 0;i < m;i++) for(int j = 0;j < n + 1;j++) C[i][j] = in.nextInt(); test.getResult(); } }