CF BUDDY
← Problems·

1627E · Not Escaping

2200 · data structures, dp, implementation

Problem: Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.

The building consists of nn floors, each with mm rooms each. Let (i,j)(i, j) represent the jj-th room on the ii-th floor. Additionally, there are kk ladders installed. The ii-th ladder allows Ram to travel from (ai,bi)(a_i, b_i) to (ci,di)(c_i, d_i), but not in the other direction. Ram also gains hih_i health points if he uses the ladder ii. It is guaranteed ai<cia_i < c_i for all ladders.

If Ram is on the ii-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from (i,j)(i, j) to (i,k)(i, k), he loses jkxi|j-k| \cdot x_i health points.

Ram enters the building at (1,1)(1, 1) while his helicopter is waiting at (n,m)(n, m). What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.

Input Format: The first line of input contains tt (1t51041 \leq t \leq 5 \cdot 10^4) — the number of test cases.

The first line of each test case consists of 33 integers n,m,kn, m, k (2n,m1052 \leq n, m \leq 10^5; 1k1051 \leq k \leq 10^5) — the number of floors, the number of rooms on each floor and the number of ladders respectively.

The second line of a test case consists of nn integers x1,x2,,xnx_1, x_2, \dots, x_n (1xi1061 \leq x_i \leq 10^6).

The next kk lines describe the ladders. Ladder ii is denoted by ai,bi,ci,di,hia_i, b_i, c_i, d_i, h_i (1ai<cin1 \leq a_i < c_i \leq n; 1bi,dim1 \leq b_i, d_i \leq m; 1hi1061 \leq h_i \leq 10^6)  — the rooms it connects and the health points gained from using it.

It is guaranteed ai<cia_i < c_i for all ladders and there is at most one ladder between any 2 rooms in the building.

The sum of nn, the sum of mm, and the sum of kk over all test cases do not exceed 10510^5.

Output Format: Output the minimum health Ram loses on the optimal path from (1,1)(1, 1) to (n,m)(n, m). If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).

Note: The figure for the first test case is in the statement. There are only 22 possible paths to (n,m)(n, m):

  • Ram travels to (1,3)(1, 3), takes the ladder to (3,3)(3, 3), travels to (3,2)(3, 2), takes the ladder to (5,1)(5, 1), travels to (5,3)(5, 3) where he finally escapes via helicopter. The health lost would be \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*}
  • Ram travels to (1,3)(1, 3), takes the ladder to (3,3)(3, 3), travels to (3,1)(3, 1), takes the ladder to (5,2)(5, 2), travels to (5,3)(5, 3) where he finally escapes via helicopter. The health lost would be \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*}

In the second test case, there is no path to (n,m)(n, m).

In the third case case, Ram travels to (1,3)(1, 3) and takes the only ladder to (5,3)(5, 3). He loses 525 \cdot 2 health points and gains h1=100h_1 = 100 health points. Therefore the total loss is 10100=9010-100=-90 (negative implies he gains health after the path).

Sample Cases

Case 1

Input

4
5 3 3
5 17 8 1 4
1 3 3 3 4
3 1 5 2 5
3 2 5 1 6
6 3 3
5 17 8 1 4 2
1 3 3 3 4
3 1 5 2 5
3 2 5 1 6
5 3 1
5 17 8 1 4
1 3 5 3 100
5 5 5
3 2 3 7 5
3 5 4 2 1
2 2 5 4 5
4 4 5 2 3
1 2 4 2 2
3 3 5 2 4

Output

16
NO ESCAPE
-90
27

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback