#P4473. FlyFly Nation Gathering

    ID: 17719 Type: Default 1000ms 256MiB

FlyFly Nation Gathering

FlyFly Nation Gathering

In FlyFly Nation, the land is organized as an \(N \times M\) rectangular grid where each cell represents a block. Every block is equipped with a launcher with two parameters: a cost \(A_{i,j}\) and a jump ability \(B_{i,j}\). A FlyFly can use the launcher at cell \((i,j)\) by paying a fee of \(A_{i,j}\) and then can jump to any block \((p,q)\) as long as the Manhattan distance satisfies \(|p-i|+|q-j| \le B_{i,j}\) (note that the distance between two adjacent blocks is 1).

There are three heroes, named X, Y, and Z, each initially located at a given block in the grid. They want to meet up at one of their starting positions. When a hero uses a launcher, the cost is incurred. A hero might need to use several jumps (and thus pay several fees) to reach the meeting block.

Your task is to determine at which hero's starting position they should gather so that the total cost incurred by all three heroes is minimized. In the event of a tie, the meeting position with higher priority is chosen in the order: X first, then Y, and finally Z.

Note: The grid indices are 1-indexed.

inputFormat

The input consists of the following parts:

  1. The first line contains two integers \(N\) and \(M\) representing the number of rows and columns of the grid.
  2. The next \(N\) lines each contain \(M\) integers. The \(j\)-th integer in the \(i\)-th line represents \(A_{i,j}\), the cost of using the launcher at block \((i,j)\).
  3. The following \(N\) lines each contain \(M\) integers. The \(j\)-th integer in the \(i\)-th line represents \(B_{i,j}\), the jump ability at block \((i,j)\). With this ability, from block \((i,j)\) a hero may jump to any \((p, q)\) such that \(|p-i|+|q-j| \le B_{i,j}\).
  4. The last 3 lines each contain two integers. These represent the starting coordinates (row and column) for heroes X, Y, and Z respectively.

outputFormat

Output a single line with the meeting hero's name (one of X, Y, or Z) and the minimum total cost needed for all heroes to reach that meeting position. The name and the cost are separated by a single space.

sample

2 2
1 2
3 4
2 2
2 2
1 1
1 2
2 2
Z 3