QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 1024 MB Total points: 100

#6113. 窗口排列

Statistics

KAIST 的预算快用完了——他们需要钱!他们认为宿舍楼与 KAIST 的其他建筑相比过于豪华;他们计划卖掉所有的宿舍楼,并建造一栋全新的、完全不讲究美感的建筑。

新的宿舍楼将呈网格状——没有比这更枯燥的了——大小为 $N \times M$,每个单元格都是学生居住的房间。我们打算增加一些窗户,因为我们希望学生在白天能得到一些阳光!

我们计划在房间 $(i, j)$ 中设置恰好 $w_{i,j}$ 个窗户。每个房间都被网格的 4 条单位边包围。窗户可以建在网格的单位边一侧,每条单位边的每一侧最多只能建一个窗户——因此每个单元格有 0 到 4 个窗户。窗户是单向的:单位边另一侧的窗户不计入该房间的窗户数量。

不幸的是,当学生的私人空间通过窗户被他人窥视时,他们会感到极度不适。总不适度是所有学生对 $\{a, b\}$ 的数量,使得 $a$ 和 $b$ 可以通过窗户看到对方的私人空间。

换句话说,如果一条单位边两侧都有窗户,总不适度就会增加共享该窗户的两个房间中居住人数的乘积。

给定房间 $(i, j)$ 计划设置的窗户数量 $w_{i,j}$,以及房间 $(i, j)$ 中居住的人数 $p_{i,j}$。你的任务是找到通过合理安排窗户所能达到的最小总不适度。

输入格式

第一行包含两个整数 $N$ 和 $M$:网格的尺寸。 接下来的 $N$ 行,每行包含 $M$ 个空格分隔的整数 $p_{i,j}$。 接下来的 $N$ 行,每行包含 $M$ 个空格分隔的整数 $w_{i,j}$。

  • $1 \le N \le 50$
  • $1 \le M \le 50$
  • $1 \le p_{i,j} \le 1000$ ($1 \le i \le N, 1 \le j \le M$)
  • $0 \le w_{i,j} \le 4$ ($1 \le i \le N, 1 \le j \le M$)

输出格式

输出一个整数:最小总不适度。

样例

样例输入 1

4 3
1 7 10
7 2 8
7 9 10
4 6 4
3 3 3
3 2 4
4 3 4
2 2 3

样例输出 1

178

样例输入 2

4 3
2 2 9
9 8 4
8 4 5
7 5 2
0 1 0
1 0 1
0 0 1
0 1 0

样例输出 2

0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.