#P16888. [GKS 2022 #E] Pizza Delivery
[GKS 2022 #E] Pizza Delivery
Problem Description
Ada delivers pizzas in a city consisting of a grid of horizontal and vertical streets, heading from North to South and from West to East, respectively, and numbered from to . The top left street crossing of the grid is .
Today, Ada has to deliver pizzas, one for each of customers. Each customer lives at a different street crossing; the -th customer lives at street crossing and will pay Ada coins after the pizza is delivered to their location.
Ada starts at her pizza restaurant at with coins and carrying pizzas. Her goal is to deliver all of the pizzas within 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 pizzas to their respective customers within minutes. It takes minute to walk between 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 , where is the -th row from the top, and is the -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 .
- Go east, she reaches street crossing .
- Go west, she reaches street crossing .
- Go south, she reaches street crossing .
- Stay at street crossing .
:::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 for $d \in \{\text{North}, \text{East}, \text{West}, \text{South}\}$ returns the amount of coins Ada will have after moving in the direction and is defined as follows:
where is the current number of coins that Ada has and is an operator and is a fixed positive integer. The allowed operators are:
- (addition),
- (subtraction),
- (multiplication),
- (integer division).
For example, we can have , , , .
That means that if Ada moves North one street then she will have more coins; if Ada moves East then Ada's coins will quadruple; if Ada moves West then she loses 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,
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 pizzas in minutes and, if so, the maximum number of coins Ada could have after minutes.
Input Format
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains , , , , 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 lines denote the toll functions for North, East, West, South respectively. Each of these lines contains , denoting the operator (one of , , , ), and , the positive integer used in toll function.
The following lines describe the customers. Each of these lines consists of integers , , denoting the row number of the -th customer from the top of the grid, the column number of the -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 is the test case number starting from and is IMPOSSIBLE if all pizzas cannot be delivered within minutes; otherwise, the output should be the maximum number of coins Ada can have after 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 #, Ada does not deliver any pizzas. Ada is located at street crossing . In minute she can decide to move west and go to , so that the toll at calculates Ada's coins using the toll function , which results in coins. Therefore Ada can have maximum of coins with her after minute.
In Sample Case #, Ada does not deliver any pizzas. Ada is located at street crossing . All directions have a similar toll function, for all . If she decides to go in any direction, she will end up with coins. It is optimal for Ada to stay at the same location and have coins at the end.
:::align{center}
:::
In Additional Sample Case #, Ada started at street crossing with coins. Ada can receive maximum coins by following the steps below:
- Go west to . Using the toll function for moving west , Ada now has coins.
- Do not deliver the pizza at yet, and go south to . Using the toll function for moving south , Ada now has coins.
- Go north to . Using the toll function for moving north , Ada now has coins.
- Deliver the pizza at . Ada receives additional coins for delivering the pizza. Ada has a total of coins in the end.
In Additional Sample Case #, Ada cannot deliver pizzas in minute, so the output is IMPOSSIBLE.
In Additional Sample Case #, Ada started at street crossing with coins. Ada can receive maximum coins by following the steps below:
- Go west to . Using the toll function for moving west , Ada now has coins.
- Go south to . Using the toll function for moving south , Ada now has coins.
- Deliver the pizza at . Ada receives additional coins for delivering the pizza. Ada has a total of coins in the end.
Limits
.
.
.
.
, for all .
, for all .
, for all .
is one of , for all .
It is guaranteed that no customer lives at the same street crossing as the pizza restaurant Ada starts her trip, i.e. , for all .
It is guaranteed that every customer lives at a different street crossing, i.e. , for all and .
Test Set
.
Test Set
.