CF2194E.The Turtle Strikes Back

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

After a heavy training session, Michelangelo and Raphael decided to order pizza. Today, on the occasion of the holiday, the pizzeria is preparing rectangular pizzas made of nn rows and mm columns of slices. Each slice has its own unique recipe and flavor.

Michelangelo was the first to open the box and roughly estimated the pleasure of eating each slice: the pleasure from the slice located in the ii-th row and jj-th column is equal to ai,ja_{i,j}. It is guaranteed that there is at least one slice that Michelangelo liked, meaning that among ai,ja_{i, j} there is at least one non-negative number.

The turtles agreed that Michelangelo would eat first. According to an ancient tradition, he must choose a route from the top left corner (1,1)(1,1) to the bottom right corner (n,m)(n,m) and eat all the slices on that route. In one step, he can move either to the adjacent slice on the right or to the adjacent slice below.

Michelangelo aims to maximize the total pleasure from the eaten slices.

However, Raphael decided to use sauce. Before Michelangelo chooses a route, Raphael decided to choose exactly one slice of pizza and apply his signature sauce to it. But due to the specifics of this sauce, the pleasure from that slice changes to the opposite: if it was previously ai,ja_{i,j}, after applying the sauce it becomes −ai,j-a_{i,j}.

After that, knowing Raphael's choice, Michelangelo will choose the optimal route for himself and eat all the slices on it.

Raphael became curious about what minimum pleasure Michelangelo could achieve. Help him calculate this number.

经过一场高强度训练后,米开朗基罗和拉斐尔决定点一份披萨。今天恰逢节日,披萨店特制了矩形披萨,由 nn 行 mm 列的披萨块组成。每一块披萨都有其独特的配方与风味。

米开朗基罗最先打开披萨盒,并粗略估计了每一块披萨带来的愉悦值:位于第 ii 行第 jj 列的披萨块带来的愉悦值为 ai,ja_{i,j}。题目保证至少存在一块米开朗基罗喜欢的披萨块,即所有 ai,ja_{i, j} 中至少有一个非负数。

两只乌龟约定由米开朗基罗先吃。根据一项古老传统,他必须选择一条从左上角 (1,1)(1,1) 到右下角 (n,m)(n,m) 的路径,并吃掉该路径上的所有披萨块。每一步,他只能向右移动到相邻的披萨块,或向下移动到相邻的披萨块。

米开朗基罗的目标是使所吃披萨块的总愉悦值最大化。

然而,拉斐尔决定使用他的特制酱料。在米开朗基罗选定路径之前,拉斐尔将恰好选择一块披萨并为其涂上他的招牌酱料。但由于这种酱料的特殊性质,该块披萨的愉悦值会变为相反数:若原来为 ai,ja_{i,j},涂酱后则变为 −ai,j-a_{i,j}。

之后,米开朗基罗在已知拉斐尔的选择的前提下,会选择一条对自己最优的路径,并吃掉该路径上的所有披萨块。

拉斐尔很好奇:米开朗基罗最终所能获得的最小可能愉悦值是多少?请帮助他计算这个数值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

In the first line of each test case, two integers n,m (1≤n,m≤106,1≤n⋅m≤106)n, m\, (1 \le n, m \le 10^6, 1 \leq n \cdot m \le 10^6) are given — the number of rows and columns in the table, respectively.

In each of the following nn lines of each test case, mm integers are provided, separated by spaces. The jj-th number in the ii-th of these lines corresponds to the value ai,ja_{i, j} (−109≤ai,j≤109-10^9 \leq a_{i, j} \leq 10^9). It is guaranteed that there is at least one non-negative value among the ai,ja_{i, j}.

It is guaranteed that the sum of n⋅mn \cdot m across all test cases does not exceed 10610^6

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

在每个测试用例的第一行中,给出两个整数 n,mn, m(1≤n,m≤1061 \le n, m \le 10^6,且 1≤n⋅m≤1061 \leq n \cdot m \le 10^6),分别表示表格的行数和列数。

在每个测试用例接下来的 nn 行中,每行给出 mm 个由空格分隔的整数。其中第 ii 行中的第 jj 个数对应值 ai,ja_{i, j}(−109≤ai,j≤109-10^9 \leq a_{i, j} \leq 10^9)。保证所有 ai,ja_{i, j} 中至少存在一个非负值。

保证所有测试用例中 n⋅mn \cdot m 的总和不超过 10610^6。

输出格式

For each test case, output a single integer — the minimum pleasure Michelangelo can achieve, which Raphael can guarantee.

对于每个测试用例,输出一个整数——米开朗基罗所能获得的最小愉悦值,该值是拉斐尔能够保证的。

输入输出样例

  • 输入#1

    2
    3 3
    1 -2 3
    4 -5 2
    1 6 -1
    2 4
    -1 -1 -1 1
    -1 -1 -1 -1

    输出#1

    3
    -5

说明/提示

If Raphael did not use the sauce, Michelangelo would choose the path $$ (1,1) \rightarrow (2,1) \rightarrow(3,1) \rightarrow (3,2) \rightarrow (3,3) $$ which gives a total pleasure of 1+4+1+6−1=111 + 4 + 1 + 6 - 1 = 11.

However, if Raphael applies the sauce to the cell (3,2)(3,2), the value 66 will turn into −6-6. Then the optimal path for Michelangelo will be $$ (1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) \rightarrow (3,3) $$ with a total pleasure of 1−2+3+2−1=31 - 2 + 3 + 2 - 1 = 3. Raphael cannot achieve a lower result in any cell

如果拉斐尔不使用酱料,米开朗基罗将选择路径

(1,1)→(2,1)→(3,1)→(3,2)→(3,3)(1,1) \rightarrow (2,1) \rightarrow(3,1) \rightarrow (3,2) \rightarrow (3,3)

该路径带来的总愉悦值为 1+4+1+6−1=111 + 4 + 1 + 6 - 1 = 11。

然而,若拉斐尔将酱料施用于格子 (3,2)(3,2),其值 66 将变为 −6-6。此时米开朗基罗的最优路径将变为

(1,1)→(1,2)→(1,3)→(2,3)→(3,3)(1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) \rightarrow (3,3)

该路径带来的总愉悦值为 1−2+3+2−1=31 - 2 + 3 + 2 - 1 = 3。拉斐尔无法通过在任意其他格子施用酱料来得到更低的结果。

输入解题思路,AI测评打分。不知道怎么写?

首页