#P16888. [GKS 2022 #E] Pizza Delivery

    ID: 19216 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2022状压 DPGoogle Kick Start

[GKS 2022 #E] Pizza Delivery

Problem Description

Ada delivers pizzas in a city consisting of a grid of NN horizontal and NN vertical streets, heading from North to South and from West to East, respectively, and numbered from 11 to NN. The top left street crossing of the grid is (1,1)(1, 1).

Today, Ada has to deliver PP pizzas, one for each of PP customers. Each customer lives at a different street crossing; the kk-th customer lives at street crossing (Xk,Yk)(X_k, Y_k) and will pay Ada CkC_k coins after the pizza is delivered to their location.

Ada starts at her pizza restaurant at (Ar,Ac)(A_r, A_c) with 00 coins and carrying PP pizzas. Her goal is to deliver all of the pizzas within MM minutes. She is free to take any path she likes around the city and finish deliveries anywhere, as long as she manages to drop off all PP pizzas to their respective customers within MM minutes. It takes 11 minute to walk between 22 adjacent street crossings, and takes no additional time to drop off a pizza at a customer's location. There are some additional rules and constraints to note:

  • Ada is not allowed to go outside the grid.
  • No customer lives at the same street crossing as the pizza restaurant Ada starts her trip.
  • At any point in time Ada can choose to stay in her current location and not move.
  • Ada can also choose not to deliver a pizza when at a customer's location.

Formally, if Ada is currently at street crossing (i,j)(i, j), where ii is the ii-th row from the top, and jj is the jj-th column from the left, she can decide to do any of the following as long as she does not go outside the grid:

  • Go north, she reaches street crossing (i1,j)(i - 1, j).
  • Go east, she reaches street crossing (i,j+1)(i, j + 1).
  • Go west, she reaches street crossing (i,j1)(i, j - 1).
  • Go south, she reaches street crossing (i+1,j)(i + 1, j).
  • Stay at street crossing (i,j)(i, j).

:::align{center} :::

The city has a unique toll system in place for using the streets. There is a toll for using each street and the toll depends on Ada's current number of coins and the direction in which she is travelling to. The toll function is defined for every cardinal direction (North, East, West, South) separately. The toll function FdF_d for $d \in \{\text{North}, \text{East}, \text{West}, \text{South}\}$ returns the amount of coins Ada will have after moving in the direction dd and is defined as follows:

Fd=c OPd KdF_d = c \ \mathrm{OP}_d \ K_d

where cc is the current number of coins that Ada has and OPd\mathrm{OP}_d is an operator and KdK_d is a fixed positive integer. The allowed operators are:

  • ++ (addition),
  • - (subtraction),
  • ×\times (multiplication),
  • // (integer division).

For example, we can have FNorth=c+3F_{\text{North}} = c + 3, FEast=c×4F_{\text{East}} = c \times 4, FWest=c4F_{\text{West}} = c - 4, FSouth=c/2F_{\text{South}} = c / 2.

That means that if Ada moves North one street then she will have 33 more coins; if Ada moves East then Ada's coins will quadruple; if Ada moves West then she loses 44 coins; and if Ada moves South then her coins are halved.

All divisions are integer divisions and are computed by using floor function. For example,

14=14=1.-\frac{1}{4} = \lfloor -\frac{1}{4} \rfloor = -1.

Notice that Ada is allowed to have a negative number of coins. Note that the tolls might actually give Ada coins.

Find out if Ada can deliver all the PP pizzas in MM minutes and, if so, the maximum number of coins Ada could have after MM minutes.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains NN, PP, MM, ArA_r, AcA_c denoting the grid size, the number of pizzas to deliver, the minutes in which all pizzas should be delivered, and the coordinates of the street crossing at which Ada starts respectively.

The next 44 lines denote the toll functions for North, East, West, South respectively. Each of these lines contains OPd\mathrm{OP}_d, denoting the operator (one of ++, -, *, //), and KdK_d, the positive integer used in toll function.

The following PP lines describe the customers. Each of these lines consists of 33 integers XkX_k, YkY_k, CkC_k denoting the row number of the kk-th customer from the top of the grid, the column number of the kk-th customer from the left of the grid, and the amount of coins they pay on delivery, respectively.

Output Format

For each test case, output one line containing Case #x: y, where xx is the test case number starting from 11 and yy is IMPOSSIBLE if all pizzas cannot be delivered within MM minutes; otherwise, the output should be the maximum number of coins Ada can have after MM minutes, which could be negative.

2
3 0 1 1 2
+ 1
- 2
+ 3
/ 4
3 0 1 2 3
- 2
- 2
- 2
- 2
Case #1: 3
Case #2: 0
3
3 1 3 1 3
+ 4
- 2
* 1
/ 4
1 2 4
2 2 1 1 2
+ 2
+ 3
* 2
* 1
1 1 4
2 2 1
3 1 2 1 3
+ 1
* 1
- 3
/ 4
2 2 2
Case #1: 8
Case #2: IMPOSSIBLE
Case #3: 1

Hint

In Sample Case #11, Ada does not deliver any pizzas. Ada is located at street crossing (1,2)(1, 2). In 11 minute she can decide to move west and go to (1,1)(1, 1), so that the toll at (1,2)(1, 2) calculates Ada's coins using the toll function FWest=c+3F_{\text{West}} = c + 3, which results in 33 coins. Therefore Ada can have maximum of 33 coins with her after 11 minute.

In Sample Case #22, Ada does not deliver any pizzas. Ada is located at street crossing (2,3)(2, 3). All directions have a similar toll function, Fd=c2F_d = c - 2 for all dd. If she decides to go in any direction, she will end up with 2-2 coins. It is optimal for Ada to stay at the same location and have 00 coins at the end.

:::align{center} :::

In Additional Sample Case #11, Ada started at street crossing (1,3)(1, 3) with 00 coins. Ada can receive maximum coins by following the steps below:

  • Go west to (1,2)(1, 2). Using the toll function for moving west Fwest=c×1F_{\text{west}} = c \times 1, Ada now has 0×1=00 \times 1 = 0 coins.
  • Do not deliver the pizza at (1,2)(1, 2) yet, and go south to (2,2)(2, 2). Using the toll function for moving south FSouth=c/4F_{\text{South}} = c / 4, Ada now has 0/4=00 / 4 = 0 coins.
  • Go north to (1,2)(1, 2). Using the toll function for moving north FNorth=c+4F_{\text{North}} = c + 4, Ada now has 0+4=40 + 4 = 4 coins.
  • Deliver the pizza at (1,2)(1, 2). Ada receives 44 additional coins for delivering the pizza. Ada has a total of 88 coins in the end.

In Additional Sample Case #22, Ada cannot deliver 22 pizzas in 11 minute, so the output is IMPOSSIBLE.

In Additional Sample Case #33, Ada started at street crossing (1,3)(1, 3) with 00 coins. Ada can receive maximum coins by following the steps below:

  • Go west to (1,2)(1, 2). Using the toll function for moving west FWest=c3F_{\text{West}} = c - 3, Ada now has 03=30 - 3 = -3 coins.
  • Go south to (2,2)(2, 2). Using the toll function for moving south FSouth=c/4F_{\text{South}} = c / 4, Ada now has 3/4=34=1-3 / 4 = \lfloor \frac{-3}{4} \rfloor = -1 coins.
  • Deliver the pizza at (2,2)(2, 2). Ada receives 22 additional coins for delivering the pizza. Ada has a total of 11 coins in the end.

Limits

1T1001 \le T \le 100.

1N101 \le N \le 10.

1M201 \le M \le 20.

1Ar,AcN1 \le A_r, A_c \le N.

1Xk,YkN1 \le X_k, Y_k \le N, for all kk.

1Ck41 \le C_k \le 4, for all kk.

1Kd41 \le K_d \le 4, for all dd.

OPd\mathrm{OP}_d is one of (+,,,/)(+, -, *, /), for all dd.

It is guaranteed that no customer lives at the same street crossing as the pizza restaurant Ada starts her trip, i.e. (Xk,Yk)(Ar,Ac)(X_k, Y_k) \ne (A_r, A_c), for all 1kP1 \le k \le P.

It is guaranteed that every customer lives at a different street crossing, i.e. (Xk,Yk)(Xl,Yl)(X_k, Y_k) \ne (X_l, Y_l), for all 1k,lP1 \le k, l \le P and klk \ne l.

Test Set 11

P=0P = 0.

Test Set 22

0P100 \le P \le 10.