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 n n floors, each with m m m rooms each. Let ( i , j ) (i, j) (i,j) represent the j j j-th room on the i i i-th floor. Additionally, there are k k k ladders installed. The i i i-th ladder allows Ram to travel from ( a i , b i ) (a_i, b_i) (ai,bi) to ( c i , d i ) (c_i, d_i) (ci,di), but not in the other direction. Ram also gains h i h_i hi health points if he uses the ladder i i i. It is guaranteed a i < c i a_i < c_i ai<ci for all ladders.
If Ram is on the i i i-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from ( i , j ) (i, j) (i,j) to ( i , k ) (i, k) (i,k), he loses ∣ j − k ∣ ⋅ x i |j-k| \cdot x_i ∣j−k∣⋅xi health points.
Ram enters the building at ( 1 , 1 ) (1, 1) (1,1) while his helicopter is waiting at ( n , m ) (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
The first line of input contains t t t ( 1 ≤ t ≤ 5 ⋅ 1 0 4 1 \leq t \leq 5 \cdot 10^4 1≤t≤5⋅104) — the number of test cases.
The first line of each test case consists of 3 3 3 integers n , m , k n, m, k n,m,k ( 2 ≤ n , m ≤ 1 0 5 2 \leq n, m \leq 10^5 2≤n,m≤105; 1 ≤ k ≤ 1 0 5 1 \leq k \leq 10^5 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 n n integers x 1 , x 2 , … , x n x_1, x_2, \dots, x_n x1,x2,…,xn ( 1 ≤ x i ≤ 1 0 6 1 \leq x_i \leq 10^6 1≤xi≤106).
The next k k k lines describe the ladders. Ladder i i i is denoted by a i , b i , c i , d i , h i a_i, b_i, c_i, d_i, h_i ai,bi,ci,di,hi ( 1 ≤ a i < c i ≤ n 1 \leq a_i < c_i \leq n 1≤ai<ci≤n; 1 ≤ b i , d i ≤ m 1 \leq b_i, d_i \leq m 1≤bi,di≤m; 1 ≤ h i ≤ 1 0 6 1 \leq h_i \leq 10^6 1≤hi≤106) — the rooms it connects and the health points gained from using it.
It is guaranteed a i < c i a_i < c_i ai<ci for all ladders and there is at most one ladder between any 2 rooms in the building.
The sum of n n n, the sum of m m m, and the sum of k k k over all test cases do not exceed 1 0 5 10^5 105.
Output
Output the minimum health Ram loses on the optimal path from ( 1 , 1 ) (1, 1) (1,1) to ( n , m ) (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).
Input
45 3 35 17 8 1 41 3 3 3 43 1 5 2 53 2 5 1 66 3 35 17 8 1 4 21 3 3 3 43 1 5 2 53 2 5 1 65 3 15 17 8 1 41 3 5 3 1005 5 53 2 3 7 53 5 4 2 12 2 5 4 54 4 5 2 31 2 4 2 23 3 5 2 4
Output
16
NO ESCAPE
-90
27
Note
The figure for the first test case is in the statement. There are only 2 2 2 possible paths to ( n , m ) (n, m) (n,m):
Ram travels to ( 1 , 3 ) (1, 3) (1,3), takes the ladder to ( 3 , 3 ) (3, 3) (3,3), travels to ( 3 , 2 ) (3, 2) (3,2), takes the ladder to ( 5 , 1 ) (5, 1) (5,1), travels to ( 5 , 3 ) (5, 3) (5,3) where he finally escapes via helicopter. The health lost would be
= x 1 ⋅ ∣ 1 − 3 ∣ − h 1 + x 3 ⋅ ∣ 3 − 2 ∣ − h 3 + x 5 ⋅ ∣ 1 − 3 ∣ = 5 ⋅ 2 − 4 + 8 ⋅ 1 − 6 + 4 ⋅ 2 = 16. \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*} =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) (1,3), takes the ladder to ( 3 , 3 ) (3, 3) (3,3), travels to ( 3 , 1 ) (3, 1) (3,1), takes the ladder to ( 5 , 2 ) (5, 2) (5,2), travels to ( 5 , 3 ) (5, 3) (5,3) where he finally escapes via helicopter. The health lost would be
= x 1 ⋅ ∣ 1 − 3 ∣ − h 1 + x 3 ⋅ ∣ 3 − 1 ∣ − h 2 + a 5 ⋅ ∣ 2 − 3 ∣ = 5 ⋅ 2 − 4 + 8 ⋅ 2 − 5 + 4 ⋅ 1 = 21. \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*} =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 16 16 16.
In the second test case, there is no path to ( n , m ) (n, m) (n,m).
In the third case case, Ram travels to ( 1 , 3 ) (1, 3) (1,3) and takes the only ladder to ( 5 , 3 ) (5, 3) (5,3). He loses 5 ⋅ 2 5 \cdot 2 5⋅2 health points and gains h 1 = 100 h_1 = 100 h1=100 health points. Therefore the total loss is 10 − 100 = − 90 10-100=-90 10−100=−90 (negative implies he gains health after the path).
题目描述了一个楼层上有多个房间的建筑,Ram需要从建筑的第1层的第1个房间出发,最终到达第n层的第m个房间,并且尽量减少失去的健康点。建筑中存在多个单向梯子,这些梯子可以帮助Ram从低楼层跳跃到高楼层,且使用梯子时会有健康点的增加。
简化问题:
动态规划:
维护状态:
dp[i]
表示到达房间i所需的最小健康消耗。路径更新:
终止条件:
dp[n][m]
有值,则输出该值,否则输出“NO ESCAPE”。#include
#define endl '\n'
#define int long long
#define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
const int inf = -1e18 + 7;
const int N = 1e6 + 10;
int id;
int n, m, k;
vector<int> x(N + 1);
vector<int> dp(N + 1);
struct Node {
int y;
int h;
int id;
int to;
};
vector<Node> node[N + 1];
int cmp(Node x, Node y) {
return x.y < y.y;
}
void clear1() {
id = 0;
for (int i = 0; i <= n; ++i) {
node[i].clear();
}
}
void clear2() {
for (int i = 0; i <= id; ++i) {
dp[i] = inf;
}
dp[1] = 0;
}
void solved() {
cin >> n >> m >> k;
clear1();
for (int i = 1; i <= n; ++i) {
cin >> x[i];
}
node[1].push_back({1, 0, ++id, -1});
for (int i = 0; i < k; ++i) {
int a, b, c, d, h;
cin >> a >> b >> c >> d >> h;
node[a].push_back({b, h, ++id, id + 1});
node[c].push_back({d, 0, ++id, -1});
}
node[n].push_back({m, 0, ++id, -1});
clear2();
for (int i = 1; i <= n; ++i) {
sort(node[i].begin(), node[i].end(), cmp);
for (int j = 1; j < node[i].size(); ++j) {
dp[node[i][j].id] = max(dp[node[i][j].id], dp[node[i][j - 1].id] - (node[i][j].y - node[i][j - 1].y) * x[i]);
}
for (int j = node[i].size() - 2; j >= 0; --j) {
dp[node[i][j].id] = max(dp[node[i][j].id], dp[node[i][j + 1].id] - (node[i][j + 1].y - node[i][j].y) * x[i]);
}
for (int j = 0; j < node[i].size(); ++j) {
if (dp[node[i][j].id] != inf && node[i][j].to != -1) {
dp[node[i][j].to] = max(dp[node[i][j].to], dp[node[i][j].id] + node[i][j].h);
}
}
}
if (dp[id] == inf) {
cout << "NO ESCAPE" << endl;
} else {
cout << -dp[id] << endl;
}
}
signed main() {
BoBoowen;
int T = 1;
cin >> T;
while (T--) {
solved();
}
}
输入与初始化:
n
、房间数量m
以及梯子数量k
。梯子处理:
a_i
的房间b_i
跳跃到楼层c_i
的房间d_i
,并给Ram带来h_i
的健康奖励。我们将这些信息存储在node
数组中。动态规划处理:
dp
数组计算出最小健康消耗。结果输出:
O(k log k)
,适用于题目给出的最大输入限制。