QOJ.ac

QOJ

Time Limit: 3.0 s Memory Limit: 256 MB Total points: 100

#11630. 简单的 APSP 问题

Statistics

给定一个 $H \times W$ 的网格。左上角方格的坐标为 $(0, 0)$,右下角方格的坐标为 $(H - 1, W - 1)$。

有 $N$ 个方格 $(x_1, y_1), (x_2, y_2), \dots, (x_N, y_N)$ 被涂成黑色,其余所有方格均为白色。

定义两个白色方格 $A$ 和 $B$ 之间的最短距离为仅经过白色方格从 $A$ 到达 $B$ 所需的最少移动步数,其中一次移动可以到达相邻(上下左右)的方格。

由于总共有 $H \times W - N$ 个白色方格,因此共有 $C_{H \times W - N}^2$ 种选择两个白色方格的方法。对于每一种选择,求出所选方格之间的最短距离,然后计算所有这些距离之和,结果对 $1\,000\,000\,007 = 10^9 + 7$ 取模。

输入格式

输入按以下格式给出:

$H \ W$ $N$ $x_1 \ y_1$ $x_2 \ y_2$ $\dots$ $x_N \ y_N$

数据范围: $1 \le H, W \le 10^6$,$1 \le N \le 30$,$0 \le x_i \le H - 1$,$0 \le y_i \le W - 1$。若 $i \neq j$,则 $x_i \neq x_j$ 或 $y_i \neq y_j$。保证至少存在一个白色方格。对于任意一对白色方格 $A$ 和 $B$,保证可以通过仅经过白色方格从 $A$ 到达 $B$。

输出格式

输出所有最短距离之和对 $10^9 + 7$ 取模的结果。

样例

样例 1

2 3
1
1 1

样例 1 输出

20

样例 2

2 3
1
1 2

样例 2 输出

16

样例 3

3 3
1
1 1

样例 3 输出

64

样例 4

4 4
4
0 1
1 1
2 1
2 2

样例 4 输出

268

样例 5

1000000 1000000
1
0 0

样例 5 输出

333211937

说明

在样例 1 中,网格如下('.' 表示白色方格,'!' 表示黑色方格):

...
.!.

我们将白色方格按字母标记如下:

ABC
D!E

我们得到(此处 $dist(A, B)$ 为 $A$ 和 $B$ 之间的最短距离): $dist(A, B) = 1, dist(A, C) = 2, dist(A, D) = 1, dist(A, E) = 3, dist(B, C) = 1, dist(B, D) = 2, dist(B, E) = 2, dist(C, D) = 3, dist(C, E) = 1, dist(D, E) = 4$,总和为 20。

在样例 2 中,我们将白色方格按字母标记如下:

ABC
DE!

我们得到: $dist(A, B) = 1, dist(A, C) = 2, dist(A, D) = 1, dist(A, E) = 2, dist(B, C) = 1, dist(B, D) = 2, dist(B, E) = 1, dist(C, D) = 3, dist(C, E) = 2, dist(D, E) = 1$,总和为 16。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#558Editorial Open集训队作业 解题报告 by 叶隽霖Qingyu2026-01-02 22:21:06 Download

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.