|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) |
| 24 | + nextLine(); |
| 25 | + return st.nextToken(); |
| 26 | + } |
| 27 | + |
| 28 | + int nextInt() throws Exception { |
| 29 | + return Integer.parseInt(nextToken()); |
| 30 | + } |
| 31 | + |
| 32 | + long nextLong() throws Exception { |
| 33 | + return Long.parseLong(nextToken()); |
| 34 | + } |
| 35 | + |
| 36 | + double nextDouble() throws Exception { |
| 37 | + return Double.parseDouble(nextToken()); |
| 38 | + } |
| 39 | + |
| 40 | + void close() throws Exception { |
| 41 | + bw.flush(); |
| 42 | + bw.close(); |
| 43 | + } |
| 44 | + |
| 45 | + void write(String content) throws Exception { |
| 46 | + bw.write(content); |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +public class Main { |
| 52 | + |
| 53 | + static IOController io; |
| 54 | + |
| 55 | + // |
| 56 | + |
| 57 | + static final int INF = (int)1e9 + 7; |
| 58 | + |
| 59 | + static final int[] dx = {1,0,-1,0}; |
| 60 | + static final int[] dy = {0,1,0,-1}; |
| 61 | + |
| 62 | + static int N, M, X, Y, A, B, C; |
| 63 | + static int[][] h; |
| 64 | + |
| 65 | + public static void main(String[] args) throws Exception { |
| 66 | + |
| 67 | + io = new IOController(); |
| 68 | + |
| 69 | + N = io.nextInt(); |
| 70 | + M = io.nextInt(); |
| 71 | + X = io.nextInt(); |
| 72 | + Y = io.nextInt(); |
| 73 | + A = io.nextInt(); |
| 74 | + B = io.nextInt(); |
| 75 | + C = io.nextInt(); |
| 76 | + h = new int[N+1][M+1]; |
| 77 | + int EX = -1, EY = -1, max = -1; |
| 78 | + for(int i=1;i<=N;i++) for(int j=1;j<=M;j++) { |
| 79 | + h[i][j] = io.nextInt(); |
| 80 | + if(h[i][j] > max) { |
| 81 | + max = h[i][j]; |
| 82 | + EX = i; |
| 83 | + EY = j; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[0]-b[0]); |
| 88 | + pq.offer(new int[]{0,X,Y}); |
| 89 | + int[][] dist = new int[N+1][M+1]; |
| 90 | + for(int i=1;i<=N;i++) Arrays.fill(dist[i], INF); |
| 91 | + dist[X][Y] = 0; |
| 92 | + while(!pq.isEmpty()) { |
| 93 | + int[] cur = pq.poll(); |
| 94 | + int d = cur[0], x = cur[1], y = cur[2]; |
| 95 | + if(d > dist[x][y]) continue; |
| 96 | + if(x == EX && y == EY) { |
| 97 | + io.write(d + "\n"); |
| 98 | + io.close(); |
| 99 | + return; |
| 100 | + } |
| 101 | + for(int i=0;i<4;i++) { |
| 102 | + int xx = x + dx[i], yy = y + dy[i]; |
| 103 | + if(xx<1 || xx>N || yy<1 || yy>M || Math.abs(h[xx][yy]-h[x][y]) > C) continue; |
| 104 | + int cost = d; |
| 105 | + if(h[xx][yy] > h[x][y]) cost += A*(h[xx][yy]-h[x][y]); |
| 106 | + if(h[xx][yy] < h[x][y]) cost += B*(h[x][y]-h[xx][yy]); |
| 107 | + if(h[xx][yy] == h[x][y]) cost++; |
| 108 | + if(dist[xx][yy] > cost) { |
| 109 | + dist[xx][yy] = cost; |
| 110 | + pq.offer(new int[]{cost,xx,yy}); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + io.write("-1"); |
| 115 | + |
| 116 | + |
| 117 | + io.close(); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | +``` |
0 commit comments