CF1621D.The Winter Hike

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Circular land is an 2n×2n2n \times 2n grid. Rows of this grid are numbered by integers from 11 to 2n2n from top to bottom and columns of this grid are numbered by integers from 11 to 2n2n from left to right. The cell (x,y)(x, y) is the cell on the intersection of row xx and column yy for 1≤x≤2n1 \leq x \leq 2n and 1≤y≤2n1 \leq y \leq 2n.

There are n2n^2 of your friends in the top left corner of the grid. That is, in each cell (x,y)(x, y) with 1≤x,y≤n1 \leq x, y \leq n there is exactly one friend. Some of the other cells are covered with snow.

Your friends want to get to the bottom right corner of the grid. For this in each cell (x,y)(x, y) with n+1≤x,y≤2nn+1 \leq x, y \leq 2n there should be exactly one friend. It doesn't matter in what cell each of friends will be.

You have decided to help your friends to get to the bottom right corner of the grid.

For this, you can give instructions of the following types:

  • You select a row xx. All friends in this row should move to the next cell in this row. That is, friend from the cell (x,y)(x, y) with 1≤y<2n1 \leq y \lt 2n will move to the cell (x,y+1)(x, y + 1) and friend from the cell (x,2n)(x, 2n) will move to the cell (x,1)(x, 1).
  • You select a row xx. All friends in this row should move to the previous cell in this row. That is, friend from the cell (x,y)(x, y) with 1<y≤2n1 \lt y \leq 2n will move to the cell (x,y−1)(x, y - 1) and friend from the cell (x,1)(x, 1) will move to the cell (x,2n)(x, 2n).
  • You select a column yy. All friends in this column should move to the next cell in this column. That is, friend from the cell (x,y)(x, y) with 1≤x<2n1 \leq x \lt 2n will move to the cell (x+1,y)(x + 1, y) and friend from the cell (2n,y)(2n, y) will move to the cell (1,y)(1, y).
  • You select a column yy. All friends in this column should move to the previous cell in this column. That is, friend from the cell (x,y)(x, y) with 1<x≤2n1 \lt x \leq 2n will move to the cell (x−1,y)(x - 1, y) and friend from the cell (1,y)(1, y) will move to the cell (2n,y)(2n, y).

Note how friends on the grid border behave in these instructions.

Example of applying the third operation to the second column. Here, colorful circles denote your friends and blue cells are covered with snow.

You can give such instructions any number of times. You can give instructions of different types. If after any instruction one of your friends is in the cell covered with snow he becomes ill.

In order to save your friends you can remove snow from some cells before giving the first instruction:

  • You can select the cell (x,y)(x, y) that is covered with snow now and remove snow from this cell for cx,yc_{x, y} coins.

You can do this operation any number of times.

You want to spend the minimal number of coins and give some instructions to your friends. After this, all your friends should be in the bottom right corner of the grid and none of them should be ill.

Please, find how many coins you will spend.

环形土地是一个 2n×2n2n \times 2n 的网格。该网格的行从上到下依次编号为 11 到 2n2n,列从左到右依次编号为 11 到 2n2n。单元格 (x,y)(x, y) 表示第 xx 行与第 yy 列相交处的单元格,其中 1≤x≤2n1 \leq x \leq 2n 且 1≤y≤2n1 \leq y \leq 2n。

你的 n2n^2 位朋友全部位于网格的左上角区域:即对所有满足 1≤x,y≤n1 \leq x, y \leq n 的单元格 (x,y)(x, y),每个单元格中恰好有一位朋友。其余某些单元格被积雪覆盖。

你的朋友们希望到达网格的右下角区域。为此,需确保每个满足 n+1≤x,y≤2nn+1 \leq x, y \leq 2n 的单元格 (x,y)(x, y) 中恰好有一位朋友(朋友在右下角区域内的具体分布位置不限)。

你决定帮助朋友们抵达网格右下角区域。

为此,你可以发出如下四种类型的指令:

  • 选定某一行 xx:该行中所有朋友均向右移动一格。即,位于单元格 (x,y)(x, y) 的朋友(其中 1≤y<2n1 \leq y < 2n)将移至 (x,y+1)(x, y + 1);而位于 (x,2n)(x, 2n) 的朋友将移至 (x,1)(x, 1)。
  • 选定某一行 xx:该行中所有朋友均向左移动一格。即,位于单元格 (x,y)(x, y) 的朋友(其中 1<y≤2n1 < y \leq 2n)将移至 (x,y−1)(x, y - 1);而位于 (x,1)(x, 1) 的朋友将移至 (x,2n)(x, 2n)。
  • 选定某一列 yy:该列中所有朋友均向下移动一格。即,位于单元格 (x,y)(x, y) 的朋友(其中 1≤x<2n1 \leq x < 2n)将移至 (x+1,y)(x + 1, y);而位于 (2n,y)(2n, y) 的朋友将移至 (1,y)(1, y)。
  • 选定某一列 yy:该列中所有朋友均向上移动一格。即,位于单元格 (x,y)(x, y) 的朋友(其中 1<x≤2n1 < x \leq 2n)将移至 (x−1,y)(x - 1, y);而位于 (1,y)(1, y) 的朋友将移至 (2n,y)(2n, y)。

注意:上述指令中,位于网格边界的朋友们将按“环形”方式移动(即移出边界后从对侧进入)。

对第二列执行第三种操作(向下移动)的示例图。图中彩色圆圈代表你的朋友,蓝色单元格表示被积雪覆盖的单元格。

你可以任意多次发出上述指令,也可以混合使用不同类型的指令。若在任意一次指令执行后,有朋友处于被积雪覆盖的单元格中,则该朋友将生病。

为保护朋友们,你可在发出第一条指令前清除部分单元格的积雪:

  • 你可以选定一个当前被积雪覆盖的单元格 (x,y)(x, y),并花费 cx,yc_{x, y} 枚金币将其积雪清除。

该操作可执行任意多次。

你的目标是以最少的金币花费,发出一系列指令,使得最终所有朋友均位于网格右下角区域(即所有满足 n+1≤x,y≤2nn+1 \leq x, y \leq 2n 的单元格中各恰有一位朋友),且无任何人因积雪而生病。

请计算你所需花费的最少金币数。

输入格式

The first line contains a single integer tt (1≤t≤1001 \leq t \leq 100) — the number of test cases.

The first line of each test case contains the single integer nn (1≤n≤2501 \leq n \leq 250).

Each of the next 2n2n lines contains 2n2n integers ci,1,ci,2,…,ci,2nc_{i, 1}, c_{i, 2}, \ldots, c_{i, 2n} (0≤ci,j≤1090 \leq c_{i, j} \leq 10^9) — costs of removing snow from cells. If ci,j=0c_{i, j} = 0 for some i,ji, j than there is no snow in cell (i,j)(i, j). Otherwise, cell (i,j)(i, j) is covered with snow.

It is guaranteed that ci,j=0c_{i, j} = 0 for 1≤i,j≤n1 \leq i, j \leq n.

It is guaranteed that the sum of nn over all test cases doesn't exceed 250250.

第一行包含一个整数 tt(1≤t≤1001 \leq t \leq 100)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2501 \leq n \leq 250)。

接下来的 2n2n 行中,每行包含 2n2n 个整数 ci,1,ci,2,…,ci,2nc_{i, 1}, c_{i, 2}, \ldots, c_{i, 2n}(0≤ci,j≤1090 \leq c_{i, j} \leq 10^9)—— 清除各单元格积雪的费用。若对某些 i,ji, j 有 ci,j=0c_{i, j} = 0,则单元格 (i,j)(i, j) 中无积雪;否则,单元格 (i,j)(i, j) 被积雪覆盖。

保证对所有满足 1≤i,j≤n1 \leq i, j \leq n 的 i,ji, j,均有 ci,j=0c_{i, j} = 0。

保证所有测试用例的 nn 值之和不超过 250250。

输出格式

For each test case output one integer — the minimal number of coins you should spend.

对于每个测试用例,输出一个整数——你需要花费的最少硬币数量。

输入输出样例

  • 输入#1

    4
    1
    0 8
    1 99
    2
    0 0 0 0
    0 0 0 0
    9 9 2 2
    9 9 9 9
    2
    0 0 4 2
    0 0 2 4
    4 2 4 2
    2 4 2 4
    4
    0 0 0 0 0 0 0 2
    0 0 0 0 0 0 2 0
    0 0 0 0 0 2 0 0
    0 0 0 0 2 0 0 0
    0 0 0 2 2 0 2 2
    0 0 2 0 1 6 2 1
    0 2 0 0 2 4 7 4
    2 0 0 0 2 0 1 6

    输出#1

    100
    22
    14
    42

说明/提示

In the first test case you can remove snow from the cells (2,1)(2, 1) and (2,2)(2, 2) for 100100 coins. Then you can give instructions

  • All friends in the first collum should move to the previous cell. After this, your friend will be in the cell (2,1)(2, 1).
  • All friends in the second row should move to the next cell. After this, your friend will be in the cell (2,2)(2, 2).

In the second test case you can remove all snow from the columns 33 and 44 for 2222 coins. Then you can give instructions

  • All friends in the first row should move to the next cell.
  • All friends in the first row should move to the next cell.
  • All friends in the second row should move to the next cell.
  • All friends in the second row should move to the next cell.
  • All friends in the third column should move to the next cell.
  • All friends in the third column should move to the next cell.
  • All friends in the fourth column should move to the next cell.
  • All friends in the fourth column should move to the next cell.

It can be shown that none of the friends will become ill and that it is impossible to spend less coins.

在第一个测试用例中,你可以花费 100 枚硬币清除单元格 (2,1)(2, 1) 和 (2,2)(2, 2) 上的积雪。然后你可以发出如下指令:

  • 第一列中的所有朋友向左移动一格(即移至前一个单元格)。执行后,你的朋友将位于单元格 (2,1)(2, 1)。
  • 第二行中的所有朋友向右移动一格(即移至下一个单元格)。执行后,你的朋友将位于单元格 (2,2)(2, 2)。

在第二个测试用例中,你可以花费 22 枚硬币清除第 3 列和第 4 列上的全部积雪。然后你可以发出如下指令:

  • 第一行中的所有朋友向右移动一格(即移至下一个单元格)。
  • 第一行中的所有朋友向右移动一格(即移至下一个单元格)。
  • 第二行中的所有朋友向右移动一格(即移至下一个单元格)。
  • 第二行中的所有朋友向右移动一格(即移至下一个单元格)。
  • 第三列中的所有朋友向下移动一格(即移至下一个单元格)。
  • 第三列中的所有朋友向下移动一格(即移至下一个单元格)。
  • 第四列中的所有朋友向下移动一格(即移至下一个单元格)。
  • 第四列中的所有朋友向下移动一格(即移至下一个单元格)。

可以证明:没有任何朋友会生病,且无法以更少的硬币完成该任务。

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

首页