|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | + |
| 7 | +class Main { |
| 8 | + |
| 9 | + // IO field |
| 10 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + static StringTokenizer st = new StringTokenizer(""); |
| 13 | + |
| 14 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 15 | + static int nextInt() throws Exception { |
| 16 | + if(!st.hasMoreTokens()) nextLine(); |
| 17 | + return Integer.parseInt(st.nextToken()); |
| 18 | + } |
| 19 | + static long nextLong() throws Exception { |
| 20 | + if(!st.hasMoreTokens()) nextLine(); |
| 21 | + return Long.parseLong(st.nextToken()); |
| 22 | + } |
| 23 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 24 | + |
| 25 | + // Additional field |
| 26 | + |
| 27 | + static final int INF = 1000111777; |
| 28 | + |
| 29 | + static int N, M, L, G; |
| 30 | + static int[][] right, down; |
| 31 | + static int[][][][] D; |
| 32 | + |
| 33 | + |
| 34 | + public static void main(String[] args) throws Exception { |
| 35 | + |
| 36 | + for(int T=nextInt();T-->0;) { |
| 37 | + |
| 38 | + ready(); |
| 39 | + solve(); |
| 40 | + } |
| 41 | + |
| 42 | + bwEnd(); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + static void ready() throws Exception{ |
| 47 | + |
| 48 | + N = nextInt(); |
| 49 | + M = nextInt(); |
| 50 | + L = nextInt(); |
| 51 | + G = nextInt(); |
| 52 | + right = new int[N][M]; |
| 53 | + down = new int[N][M]; |
| 54 | + D = new int[N][M][Math.min(N-1, M-1)*2+2][2]; |
| 55 | + |
| 56 | + for(int i=0;i<N;i++) for(int j=0;j<M-1;j++) right[i][j] = nextInt(); |
| 57 | + for(int i=0;i<N-1;i++) for(int j=0;j<M;j++) down[i][j] = nextInt(); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + static void solve() throws Exception{ |
| 62 | + |
| 63 | + dp(); |
| 64 | + int ans = INF; |
| 65 | + for(int i=0;i<Math.min(N-1, M-1)*2+2;i++) { |
| 66 | + if(Math.min(D[N-1][M-1][i][0], D[N-1][M-1][i][1]) <= G) { |
| 67 | + bw.write((L*(N+M-2)+i)+"\n"); |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + bw.write("-1\n"); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + static void dp() { |
| 76 | + |
| 77 | + for(int i=0;i<N;i++) for(int j=0;j<M;j++) for(int k=0;k<Math.min(N-1,M-1)*2+2;k++) Arrays.fill(D[i][j][k], INF); |
| 78 | + |
| 79 | + D[0][1][0][0] = right[0][0]; |
| 80 | + D[1][0][0][1] = down[0][0]; |
| 81 | + for(int i=0;i<N;i++) for(int j=0;j<M;j++) for(int k=0;k<Math.min(N-1, M-1)*2+2;k++) { |
| 82 | + if(i+j <= 1) continue; |
| 83 | + if(j>0) { |
| 84 | + D[i][j][k][0] = right[i][j-1]; |
| 85 | + int res = D[i][j-1][k][0]; |
| 86 | + if(k > 0) res = Math.min(res, D[i][j-1][k-1][1]); |
| 87 | + D[i][j][k][0] += res; |
| 88 | + } |
| 89 | + if(i>0) { |
| 90 | + D[i][j][k][1] = down[i-1][j]; |
| 91 | + int res = D[i-1][j][k][1]; |
| 92 | + if(k > 0) res = Math.min(res, D[i-1][j][k-1][0]); |
| 93 | + D[i][j][k][1] += res; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +``` |
0 commit comments