Not Escaping ( Codeforces Round 766 (Div. 2) )

Not Escaping ( Codeforces Round 766 (Div. 2) )

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 jkxi 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 1t5104) — 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 2n,m105; 1 ≤ k ≤ 1 0 5 1 \leq k \leq 10^5 1k105) — 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 1xi106).

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 1ai<cin; 1 ≤ b i , d i ≤ m 1 \leq b_i, d_i \leq m 1bi,dim; 1 ≤ h i ≤ 1 0 6 1 \leq h_i \leq 10^6 1hi106) — 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).

Example

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∣13∣h1+x3∣32∣h3+x5∣13∣=524+816+42=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∣13∣h1+x3∣31∣h2+a5∣23∣=524+825+41=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 52 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 10100=90 (negative implies he gains health after the path).

题目大意:

题目描述了一个楼层上有多个房间的建筑,Ram需要从建筑的第1层的第1个房间出发,最终到达第n层的第m个房间,并且尽量减少失去的健康点。建筑中存在多个单向梯子,这些梯子可以帮助Ram从低楼层跳跃到高楼层,且使用梯子时会有健康点的增加。

  • 在每一层,Ram可以左右移动房间,但需要付出一定的健康点代价。这个代价是根据房间间的距离以及该层的“健康消耗系数”来计算的。
  • 梯子允许Ram从某个房间跳跃到更高的楼层,但会有健康点的奖励。
  • 目标是计算Ram从(1, 1)到(n, m)的最小健康损失(注意健康损失可以是负数,表示他获得了健康点)。如果无法到达目标房间,输出“NO ESCAPE”。

解题思路:

  1. 简化问题:

    • 楼层和房间数量非常大,但我们只关心几个特定房间(例如:开始房间(1, 1),目标房间(n, m)和所有存在梯子的房间)。
    • 所以我们可以通过"房间压缩"来减小问题的规模,只保留那些对路径有影响的房间。
  2. 动态规划:

    • 使用动态规划来记录到达每个房间所需的最小健康消耗。
    • 每一层上的房间可以通过左右移动来更新健康消耗。梯子则让Ram能够跨楼层,从低楼层跳到高楼层,并带来健康点奖励。
  3. 维护状态:

    • 定义dp[i]表示到达房间i所需的最小健康消耗。
    • 对每一层,计算通过左到右和右到左两种方式更新房间之间的健康消耗。
    • 每次遇到梯子时,更新从梯子起点到终点的健康消耗。
  4. 路径更新:

    • 从1楼到达每个房间,逐步计算到达下一层时的最小健康损失。
    • 如果一个房间通过梯子能够到达更高楼层,则根据梯子的奖励更新相应房间的状态。
  5. 终止条件:

    • 如果到达目标房间(n, m)时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();
    }
}

代码分析:

  1. 输入与初始化:

    • 首先读取多个测试用例的数量。
    • 对于每个测试用例,初始化相关参数,包括楼层数量n、房间数量m以及梯子数量k
  2. 梯子处理:

    • 每个梯子可以帮助Ram从楼层a_i的房间b_i跳跃到楼层c_i的房间d_i,并给Ram带来h_i的健康奖励。我们将这些信息存储在node数组中。
  3. 动态规划处理:

    • 每一层处理时,首先根据房间的顺序更新房间间的健康损失。
    • 然后,遍历每个房间,查看是否有梯子可以使用,如果有,则更新目标房间的健康消耗。
    • 最终,通过dp数组计算出最小健康消耗。
  4. 结果输出:

    • 如果能成功到达目标房间,则输出最终的健康消耗值;如果无法到达,则输出“NO ESCAPE”。

总结:

  • 通过状态压缩和动态规划的结合,优化了问题的解法,将原本巨大的空间复杂度降到合理范围。
  • 对于每层楼的房间,使用动态规划按顺序计算最小健康损失,并处理梯子的影响。
  • 该解法的时间复杂度是O(k log k),适用于题目给出的最大输入限制。

你可能感兴趣的:(acm训练集合,dp,数据结构,模拟,最短路)