QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 256 MB Puntuación total: 100

#1188. 评估洪水风险

Estadísticas

Boat 先生拥有一大片土地。由于今年日本遭受了多次台风袭击,他开始担心自己地产的洪水风险,并希望了解土地的平均海拔。由于土地过于广阔,无法在许多地点测量海拔。考虑到地产内没有陡峭的斜坡,他认为只需测量有限数量地点的海拔,然后据此推算其余部分的海拔即可。基于相同的测量结果,可能会有多种推算方案,在这种情况下,他想知道最坏的情况,即给出最低平均海拔的那种方案。

Boat 先生的地产呈矩形,被划分为大小相同的网格状矩形区域。目前已对其中一些区域进行了海拔测量,测量结果已知。其余区域的海拔将基于“两个相邻区域的海拔差不超过 1”这一假设进行推算。

在下方给出的第一个样例中,土地被划分为 $5 \times 4$ 个区域。区域 $(1, 1)$ 和 $(5, 4)$ 的海拔分别测量为 $10$ 和 $3$。在这种情况下,基于相邻区域海拔差不超过 $1$ 的假设,所有区域的海拔是唯一确定的。

在第二个样例中,存在多种可能性,应考虑其中给出最低平均海拔的一种。

在第三个样例中,没有任何海拔分配方案满足关于海拔差的假设。

你的任务是编写一个程序来推算他地产的平均海拔。准确地说,程序应计算所有网格划分区域的推算海拔与测量海拔的总和。如果存在两种或多种不同的推算方案,程序应计算最严苛推算下的总和,即给出最低海拔总和的那种方案。

输入格式

输入的第一行包含三个整数 $w, d$ 和 $n$ ($1 \le w, d, n \le 50$)。$w$ 和 $d$ 是土地两侧的区域数量。$n$ 是测量了海拔的区域数量。

接下来的 $n$ 行中,第 $i$ 行包含三个整数 $x_i, y_i$ 和 $z_i$,满足 $1 \le x_i \le w, 1 \le y_i \le d, -100 \le z_i \le 100$。它们表示区域 $(x_i, y_i)$ 的海拔测量值为 $z_i$。同一区域最多给出一个测量结果,即对于 $i \neq j$,$(x_i, y_i) \neq (x_j, y_j)$。

输出格式

如果所有未测量区域都可以在假设两个相邻区域海拔差不超过 $1$ 的前提下,分配海拔且不与测量值冲突,则输出一个整数,表示所有区域的测量或推算海拔的总和。如果存在多种这样的海拔分配方案,则输出所有可能方案中最小的海拔总和。

如果没有海拔分配方案满足海拔差假设,则输出 “No”。

样例

样例输入 1

5 4 2
1 1 10
5 4 3

样例输出 1

130

样例输入 2

5 4 3
2 2 0
4 3 0
5 1 2

样例输出 2

-14

样例输入 3

3 3 2
1 1 8
3 3 3

样例输出 3

No

样例输入 4

2 2 1
1 1 -100

样例输出 4

-404

说明

样例 1

样例 2

样例 3

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.