[Solution] Not Escaping solution codeforces – What is the minimum amount of health Ram loses if he takes the most optimal path?

Not Escaping solution codeforces

Not Escaping solution codeforces – 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)(ai,bi) to (ci,di)(ci,di), but not in the other direction. Ram also gains hihi health points if he uses the ladder iiIt is guaranteed ai<ciai<ci 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 |jk|xi|j−k|⋅xi 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.

Not Escaping solution codeforces Input

 

The first line of input contains tt (1t51041≤t≤5⋅104) — the number of test cases.

The first line of each test case consists of 33 integers n,m,kn,m,k (2n,m1052≤n,m≤1051k1051≤k≤105) — 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,,xnx1,x2,…,xn (1xi1061≤xi≤106).

The next kk lines describe the ladders. Ladder ii is denoted by ai,bi,ci,di,hiai,bi,ci,di,hi (1ai<cin1≤ai<ci≤n1bi,dim1≤bi,di≤m1hi1061≤hi≤106)  — the rooms it connects and the health points gained from using it.

It is guaranteed ai<ciai<ci 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 105105.

Output

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).

Example
input

Copy
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 Not Escaping solution codeforces

16
NO ESCAPE
-90
27
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
    x1|13|h1+x3|32|h3+x5|13|=524+816+42=16.=x1⋅|1−3|−h1+x3⋅|3−2|−h3+x5⋅|1−3|=5⋅2−4+8⋅1−6+4⋅2=16.
  • 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
    x1|13|h1+x3|31|h2+a5|23|=524+825+41=21.=x1⋅|1−3|−h1+x3⋅|3−1|−h2+a5⋅|2−3|=5⋅2−4+8⋅2−5+4⋅1=21.

Therefore, the minimum health lost would be 1616.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⋅2 health points and gains h1=100h1=100 health points. Therefore the total loss is 10100=9010−100=−90 (negative implies he gains health after the path).

Leave a Comment

Your email address will not be published. Required fields are marked *