CF1941E.Rudolf and k Bridges

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem.

The river is a grid of nn rows and mm columns. The intersection of the ii-th row and the jj-th column contains the number ai,ja_{i,j} — the depth in the corresponding cell. All cells in the first and last columns correspond to the river banks, so the depth for them is 00.

The river may look like this.

Rudolf can choose the row (i,1),(i,2),…,(i,m)(i,1), (i,2), \ldots, (i,m) and build a bridge over it. In each cell of the row, he can install a support for the bridge. The cost of installing a support in the cell (i,j)(i,j) is ai,j+1a_{i,j}+1. Supports must be installed so that the following conditions are met:

  1. A support must be installed in cell (i,1)(i,1);
  2. A support must be installed in cell (i,m)(i,m);
  3. The distance between any pair of adjacent supports must be no more than dd. The distance between supports (i,j1)(i, j_1) and (i,j2)(i, j_2) is ∣j1−j2∣−1|j_1 - j_2| - 1.

Building just one bridge is boring. Therefore, Rudolf decided to build kk bridges on consecutive rows of the river, that is, to choose some ii (1≤i≤n−k+11 \le i \le n - k + 1) and independently build a bridge on each of the rows i,i+1,…,i+k−1i, i + 1, \ldots, i + k - 1. Help Rudolf minimize the total cost of installing supports.

伯纳德很喜欢去拜访鲁道夫,但他总是迟到。问题在于,伯纳德必须乘坐渡轮横渡一条河流。鲁道夫决定帮助他的朋友解决这个问题。

这条河流是一个 nn 行 mm 列的网格。第 ii 行第 jj 列的交叉点处有一个数值 ai,ja_{i,j},表示该格子的水深。第一列和最后一列的所有格子对应河岸,因此其水深均为 00。

河流可能长这样。

鲁道夫可以选择某一行 (i,1),(i,2),…,(i,m)(i,1), (i,2), \ldots, (i,m) 并在其上方建造一座桥。在该行的每个格子中,他都可以安装一个桥墩。在格子 (i,j)(i,j) 处安装桥墩的成本为 ai,j+1a_{i,j}+1。桥墩的安装需满足以下条件:

  1. 必须在格子 (i,1)(i,1) 安装桥墩;
  2. 必须在格子 (i,m)(i,m) 安装桥墩;
  3. 任意两个相邻桥墩之间的距离至多为 dd。桥墩 (i,j1)(i, j_1) 与 (i,j2)(i, j_2) 之间的距离定义为 ∣j1−j2∣−1|j_1 - j_2| - 1。

只建一座桥未免单调乏味。因此,鲁道夫决定在河流的 kk 个连续行上建造 kk 座桥,即选择某个 ii(满足 1≤i≤n−k+11 \le i \le n - k + 1),并在各行 i,i+1,…,i+k−1i, i + 1, \ldots, i + k - 1 上各自独立地建造一座桥。请帮助鲁道夫最小化所有桥墩的总安装成本。

输入格式

The first line contains a single integer tt (1≤t≤103)(1 \le t \le 10^3) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains four integers nn, mm, kk, and dd (1≤k≤n≤1001 \le k \le n \le 100, 3≤m≤2⋅1053 \le m \le 2 \cdot 10^5, 1≤d≤m1 \le d \le m) — the number of rows and columns of the field, the number of bridges, and the maximum distance between supports.

Then follow nn lines, ii-th line contains mm positive integers ai,ja_{i, j} (0≤ai,j≤1060 \le a_{i, j} \le 10^6, ai,1=ai,m=0a_{i, 1} = a_{i, m} = 0) — the depths of the river cells.

It is guaranteed that the sum of n⋅mn \cdot m for all sets of input data does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1031 \le t \le 10^3)——测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含四个整数 nn、mm、kk 和 dd(1≤k≤n≤1001 \le k \le n \le 100,3≤m≤2⋅1053 \le m \le 2 \cdot 10^5,1≤d≤m1 \le d \le m)——分别表示场地的行数、列数、桥梁数量以及桥墩之间的最大距离。

接下来是 nn 行,其中第 ii 行包含 mm 个正整数 ai,ja_{i, j}(0≤ai,j≤1060 \le a_{i, j} \le 10^6,且满足 ai,1=ai,m=0a_{i, 1} = a_{i, m} = 0)——表示河流各单元格的水深。

保证所有输入数据中 n⋅mn \cdot m 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single number — the minimum total cost of supports installation.

对于每个测试用例,输出一个数字——支撑结构安装的最小总成本。

输入输出样例

  • 输入#1

    5
    3 11 1 4
    0 1 2 3 4 5 4 3 2 1 0
    0 1 2 3 2 1 2 3 3 2 0
    0 1 2 3 5 5 5 5 5 2 0
    4 4 2 1
    0 3 3 0
    0 2 1 0
    0 1 2 0
    0 3 3 0
    4 5 2 5
    0 1 1 1 0
    0 2 2 2 0
    0 2 1 1 0
    0 3 2 1 0
    1 8 1 1
    0 10 4 8 4 4 2 0
    4 5 3 2
    0 8 4 4 0
    0 3 4 8 0
    0 8 1 10 0
    0 10 1 5 0

    输出#1

    4
    8
    4
    15
    14

说明/提示

In the first test case, it is most profitable to build a bridge on the second row.

It is not a top view, but side view: gray cells — bridge itself, white cells are empty, black cells — supports, blue cells — water, brown cells — river bottom.

In the second test case, it is most profitable to build bridges on the second and third rows. The supports will be placed in cells (2,3)(2, 3), (3,2)(3, 2), and on the river banks.

In the third test case the supports can be placed along the river banks.

在第一个测试用例中,最有利可图的做法是在第二行建造一座桥。

这不是俯视图,而是侧视图:灰色格子表示桥体本身,白色格子为空,黑色格子为桥墩,蓝色格子为水面,棕色格子为河床。

在第二个测试用例中,最有利可图的做法是在第二行和第三行分别建造桥梁。桥墩将建在格子 (2,3)(2, 3)、(3,2)(3, 2) 以及河岸上。

在第三个测试用例中,桥墩可以全部沿河岸布置。

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

首页