atcoder#APC001I. Simple APSP Problem

Simple APSP Problem

Score : 20002000 points

Problem Statement

You are given an H×WH \times W grid. The square at the top-left corner will be represented by (0,0)(0, 0), and the square at the bottom-right corner will be represented by (H1,W1)(H-1, W-1).

Of those squares, NN squares (x1,y1),(x2,y2),...,(xN,yN)(x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.

Let the shortest distance between white squares AA and BB be the minimum number of moves required to reach BB from AA visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.

Since there are H×WNH \times W - N white squares in total, there are (H×WN)C2_{(H \times W-N)}C_2 ways to choose two of the white squares.

For each of these (H×WN)C2_{(H \times W-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 11 000000 000000 007=109+7007=10^9+7.

Constraints

  • 1H,W1061 \leq H, W \leq 10^6
  • 1N301 \leq N \leq 30
  • 0xiH10 \leq x_i \leq H-1
  • 0yiW10 \leq y_i \leq W-1
  • If iji \neq j, then either xixjx_i \neq x_j or yiyjy_i \neq y_j.
  • There is at least one white square.
  • For every pair of white squares AA and BB, it is possible to reach BB from AA visiting only white squares.

Input

Input is given from Standard Input in the following format:

HH WW

NN

x1x_1 y1y_1

x2x_2 y2y_2

::

xNx_N yNy_N

Output

Print the sum of the shortest distances, modulo 109+710^9+7.

2 3
1
1 1
20

We have the following grid (.: a white square, #: a black square).

...
.#.

Let us assign a letter to each white square as follows:

ABC
D#E

Then, we have:

  • dist(A, B) = 11
  • dist(A, C) = 22
  • dist(A, D) = 11
  • dist(A, E) = 33
  • dist(B, C) = 11
  • dist(B, D) = 22
  • dist(B, E) = 22
  • dist(C, D) = 33
  • dist(C, E) = 11
  • dist(D, E) = 44

where dist(A, B) is the shortest distance between A and B. The sum of these distances is 2020.

2 3
1
1 2
16

Let us assign a letter to each white square as follows:

ABC
DE#

Then, we have:

  • dist(A, B) = 11
  • dist(A, C) = 22
  • dist(A, D) = 11
  • dist(A, E) = 22
  • dist(B, C) = 11
  • dist(B, D) = 22
  • dist(B, E) = 11
  • dist(C, D) = 33
  • dist(C, E) = 22
  • dist(D, E) = 11

The sum of these distances is 1616.

3 3
1
1 1
64
4 4
4
0 1
1 1
2 1
2 2
268
1000000 1000000
1
0 0
333211937