CF1627E.Not Escaping
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 n floors, each with m rooms each. Let (i,j) represent the j-th room on the i-th floor. Additionally, there are k ladders installed. The i-th ladder allows Ram to travel from (ai,bi) to (ci,di), but not in the other direction. Ram also gains hi health points if he uses the ladder i. It is guaranteed ai<ci for all ladders.
If Ram is on the i-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from (i,j) to (i,k), he loses ∣j−k∣⋅xi health points.
Ram enters the building at (1,1) while his helicopter is waiting at (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.

拉姆上尉正被他的宿敌拉格瓦追捕。拉姆必须抵达大楼顶部,才能通过直升机逃脱。然而,这座大楼正在起火。拉姆必须选择一条最优路径抵达大楼顶层,以使损失的生命值最小。
该大楼共有 n 层,每层有 m 个房间。记 (i,j) 表示第 i 层的第 j 个房间。此外,楼内共安装了 k 架梯子。第 i 架梯子允许拉姆从 (ai,bi) 移动至 (ci,di),但不可反向通行。若拉姆使用第 i 架梯子,则会获得 hi 点生命值。保证对所有梯子均有 ai<ci。
若拉姆位于第 i 层,则他可向左或向右移动。然而,楼层间的移动十分危险:若拉姆从 (i,j) 移动至 (i,k)(即在同一层内横向移动),则会损失 ∣j−k∣⋅xi 点生命值。
拉姆从 (1,1) 进入大楼,而他的直升机正停在 (n,m) 处等待。若拉姆选择最优路径,他最少会损失多少生命值?注意:该答案可能为负数(此时表示他净增生命值)。若无论拉姆选择何种路径都无法逃脱拉格瓦的追捕,则输出 "NO ESCAPE"。

输入格式
The first line of input contains t (1≤t≤5⋅104) — the number of test cases.
The first line of each test case consists of 3 integers n,m,k (2≤n,m≤105; 1≤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 n integers x1,x2,…,xn (1≤xi≤106).
The next k lines describe the ladders. Ladder i is denoted by ai,bi,ci,di,hi (1≤ai<ci≤n; 1≤bi,di≤m; 1≤hi≤106) — the rooms it connects and the health points gained from using it.
It is guaranteed ai<ci for all ladders and there is at most one ladder between any 2 rooms in the building.
The sum of n, the sum of m, and the sum of k over all test cases do not exceed 105.
输入的第一行包含 t(1≤t≤5⋅104)—— 测试用例的数量。
每个测试用例的第一行包含 3 个整数 n,m,k(2≤n,m≤105;1≤k≤105)—— 分别表示楼层数、每层的房间数以及梯子的数量。
每个测试用例的第二行包含 n 个整数 x1,x2,…,xn(1≤xi≤106)。
接下来的 k 行描述梯子。第 i 个梯子由 ai,bi,ci,di,hi(1≤ai<ci≤n;1≤bi,di≤m;1≤hi≤106)表示—— 它所连接的两个房间,以及使用该梯子所获得的生命值。
保证对所有梯子均有 ai<ci,且大楼中任意两个房间之间至多存在一条梯子。
所有测试用例的 n 之和、m 之和以及 k 之和均不超过 105。
输出格式
Output the minimum health Ram loses on the optimal path from (1,1) to (n,m). If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
输出 Ram 从 (1,1) 到 (n,m) 的最优路径上所损失的最小生命值。如果无论 Ram 选择哪条路径都无法逃脱 Raghav 的魔掌,则输出 "NO ESCAPE"(全部大写,不带引号)。
输入输出样例
输入#1
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
输出#1
16 NO ESCAPE -90 27
说明/提示
The figure for the first test case is in the statement. There are only 2 possible paths to (n,m):
- Ram travels to (1,3), takes the ladder to (3,3), travels to (3,2), takes the ladder to (5,1), travels to (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), takes the ladder to (3,3), travels to (3,1), takes the ladder to (5,2), travels to (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*} $$
Therefore, the minimum health lost would be 16.
In the second test case, there is no path to (n,m).
In the third case case, Ram travels to (1,3) and takes the only ladder to (5,3). He loses 5⋅2 health points and gains h1=100 health points. Therefore the total loss is 10−100=−90 (negative implies he gains health after the path).
第一个测试用例的示意图见题目描述。到达 (n,m) 的路径仅有 2 条:
- Ram 先移动至 (1,3),再通过梯子到达 (3,3),接着移动至 (3,2),再通过梯子到达 (5,1),最后移动至 (5,3),由此乘直升机脱险。损失的生命值为
=x1⋅∣1−3∣−h1+x3⋅∣3−2∣−h3+x5⋅∣1−3∣=5⋅2−4+8⋅1−6+4⋅2=16.
- Ram 先移动至 (1,3),再通过梯子到达 (3,3),接着移动至 (3,1),再通过梯子到达 (5,2),最后移动至 (5,3),由此乘直升机脱险。损失的生命值为
=x1⋅∣1−3∣−h1+x3⋅∣3−1∣−h2+a5⋅∣2−3∣=5⋅2−4+8⋅2−5+4⋅1=21.
因此,最小生命值损失为 16。
在第二个测试用例中,不存在通往 (n,m) 的路径。
在第三个测试用例中,Ram 移动至 (1,3) 后,使用唯一的一架梯子到达 (5,3)。他损失 5⋅2 点生命值,并获得 h1=100 点生命值。因此总损失为 10−100=−90(负值表示该路径结束后生命值净增加)。
输入解题思路,AI测评打分。不知道怎么写?