A13797 | One Billion Shades of Grey
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You have to paint with shades of grey the tiles of an $n\times n$ wall. The wall has $n$ rows of tiles, each with $n$ tiles.
The tiles on the boundary of the wall (i.e., on the first row, last row, first column and last column) are already painted and you shall not change their color. All the other tiles are not painted. Some of the tiles are broken, you shall not paint those tiles. It is guaranteed that the tiles on the boundary are not broken.
You shall paint all the non-broken tiles that are not already painted. When you paint a tile you can choose from $10^9$ shades of grey, indexed from $1$ to $10^9$ . You can paint multiple tiles with the same shade. Formally, painting the wall is equivalent to assigning a shade (an integer between $1$ and $10^9$ ) to each non-broken tile that is not already painted.
The contrast between two tiles is the absolute value of the difference between the shades of the two tiles. The total contrast of the wall is the sum of the contrast of all the pairs of adjacent non-broken tiles (two tiles are adjacent if they share a side).
Compute the minimum possible total contrast of the wall.
The tiles on the boundary of the wall (i.e., on the first row, last row, first column and last column) are already painted and you shall not change their color. All the other tiles are not painted. Some of the tiles are broken, you shall not paint those tiles. It is guaranteed that the tiles on the boundary are not broken.
You shall paint all the non-broken tiles that are not already painted. When you paint a tile you can choose from $10^9$ shades of grey, indexed from $1$ to $10^9$ . You can paint multiple tiles with the same shade. Formally, painting the wall is equivalent to assigning a shade (an integer between $1$ and $10^9$ ) to each non-broken tile that is not already painted.
The contrast between two tiles is the absolute value of the difference between the shades of the two tiles. The total contrast of the wall is the sum of the contrast of all the pairs of adjacent non-broken tiles (two tiles are adjacent if they share a side).
Compute the minimum possible total contrast of the wall.
输入格式
The first line contains $n$ ( $3\le n\le 200$ ) – the number of rows and columns.
Then $n$ lines, each containing $n$ integers, follow. The $i$ -th of these lines describe the $i$ -th row of tiles. It contains the $n$ integers $a_{ij}$ ( $-1\le a_{ij} \le 10^9)$ . The value of $a_{ij}$ described the tile on the $i$ -th row and $j$ -th column:
- If $a_{ij}=0$ , then the tile is not painted and shall be painted.
- If $a_{ij}=-1$ , then the tile is broken and shall not be painted.
- If $1\le a_{ij}\le 10^9$ , then the tile is already painted with the shade $a_{ij}$ .
It is guaranteed that the tiles on the boundary are already painted, the tiles not on the boundary are not already painted, and the tiles on the boundary are not broken.
Then $n$ lines, each containing $n$ integers, follow. The $i$ -th of these lines describe the $i$ -th row of tiles. It contains the $n$ integers $a_{ij}$ ( $-1\le a_{ij} \le 10^9)$ . The value of $a_{ij}$ described the tile on the $i$ -th row and $j$ -th column:
- If $a_{ij}=0$ , then the tile is not painted and shall be painted.
- If $a_{ij}=-1$ , then the tile is broken and shall not be painted.
- If $1\le a_{ij}\le 10^9$ , then the tile is already painted with the shade $a_{ij}$ .
It is guaranteed that the tiles on the boundary are already painted, the tiles not on the boundary are not already painted, and the tiles on the boundary are not broken.
输出格式
Print a single integer – the minimum possible total contrast of the wall.
输入输出样例
输入 #1
3 1 7 6 4 0 6 1 1 1
输出 #1
26
输入 #2
3 10 100 1 1 -1 100 10 10 10
输出 #2
396
输入 #3
5 6 6 5 4 4 6 0 0 0 4 7 0 0 0 3 8 0 0 0 2 8 8 1 2 2
输出 #3
34
输入 #4
7 315055237 841510063 581663979 148389224 405375301 243686840 882512379 683199716 -1 -1 0 0 0 346177625 496442279 0 0 0 0 0 815993623 223938231 0 0 -1 0 0 16170511 44132173 0 -1 0 0 0 130735659 212201259 0 0 -1 0 0 166102576 123213235 506794677 467013743 410119347 791447348 80193382 142887538
输出 #4
10129482893
Explanation of the first testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?):
```
<pre class="verbatim"><br></br>1 7 6<br></br>4 ? 6<br></br>1 1 1<br></br>
```
A possible way to paint the tile achieving the minimum possible contrast of $26$ is: ```
<pre class="verbatim"><br></br>1 7 6<br></br>4 5 6<br></br>1 1 1<br></br>
```
Explanation of the second testcase: Since all tiles are either painted or broken, there is nothing to do. The total contrast is $396$ .
Explanation of the third testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?):
```
<pre class="verbatim"><br></br>6 6 5 4 4<br></br>6 ? ? ? 4<br></br>7 ? ? ? 3<br></br>8 ? ? ? 2<br></br>8 8 1 2 2<br></br>
```
A possible way to paint the tiles achieving the minimum possible contrast of $34$ is: ```
<pre class="verbatim"><br></br>6 6 5 4 4<br></br>6 6 5 4 4<br></br>7 7 5 3 3<br></br>8 8 2 2 2<br></br>8 8 1 2 2<br></br>
```
```
<pre class="verbatim"><br></br>1 7 6<br></br>4 ? 6<br></br>1 1 1<br></br>
```
A possible way to paint the tile achieving the minimum possible contrast of $26$ is: ```
<pre class="verbatim"><br></br>1 7 6<br></br>4 5 6<br></br>1 1 1<br></br>
```
Explanation of the second testcase: Since all tiles are either painted or broken, there is nothing to do. The total contrast is $396$ .
Explanation of the third testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?):
```
<pre class="verbatim"><br></br>6 6 5 4 4<br></br>6 ? ? ? 4<br></br>7 ? ? ? 3<br></br>8 ? ? ? 2<br></br>8 8 1 2 2<br></br>
```
A possible way to paint the tiles achieving the minimum possible contrast of $34$ is: ```
<pre class="verbatim"><br></br>6 6 5 4 4<br></br>6 6 5 4 4<br></br>7 7 5 3 3<br></br>8 8 2 2 2<br></br>8 8 1 2 2<br></br>
```
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted