E - Delivery Robot's Patrol Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は、倉庫内で荷物を配達するロボットのプログラムを作成しています。

倉庫の床は N 行 M 列のグリッド状に区切られています。マス (r, c) は上から r 行目、左から c 列目のマスを表します(1 \leq r \leq N, 1 \leq c \leq M)。倉庫内に障害物はなく、すべてのマスを自由に通行できます。

倉庫内には K 個の配達先の荷物置き場があり、i 番目(1 \leq i \leq K)の荷物置き場はマス (R_i, C_i) にあります。荷物置き場がマス (1, 1) に存在することもあります。

ロボットは最初、グリッドの左上隅であるマス (1, 1) の充電ステーションにいます。ロボットはグリッド内で上下左右に隣接するマスへ 1 ステップで移動できます。ロボットがあるマスに到達したとき(初期位置として最初からそのマスにいる場合を含む)、そのマスに荷物置き場があれば、追加のステップなしに配達が自動的に完了します。同じマスを複数回通ることも可能です。

ロボットはすべての荷物置き場をそれぞれ少なくとも 1 回ずつ訪問して配達を完了させた後、充電のためにマス (1, 1) に戻らなければなりません。

このとき、すべての配達を完了してマス (1, 1) に戻るまでに要する合計ステップ数の最小値を求めてください。

K = 0 の場合は配達先が存在しないため、ロボットは移動する必要がなく、答えは 0 です。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 0 \leq K \leq 15
  • 1 \leq R_i \leq N (1 \leq i \leq K)
  • 1 \leq C_i \leq M (1 \leq i \leq K)
  • (R_i, C_i) \neq (R_j, C_j) (i \neq j)
  • 入力はすべて整数

入力

N M K
R_1 C_1
R_2 C_2
\vdots
R_K C_K
  • 1 行目には、グリッドの行数 N、列数 M、荷物置き場の個数 K がスペース区切りで与えられる。
  • 続く K 行のうち i 行目(1 \leq i \leq K)には、i 番目の荷物置き場の行番号 R_i と列番号 C_i がスペース区切りで与えられる。

出力

すべての荷物置き場を訪問してマス (1, 1) に戻るための最小ステップ数を 1 行で出力せよ。


入力例 1

3 3 0

出力例 1

0

入力例 2

5 5 3
1 5
5 1
3 3

出力例 2

16

入力例 3

100000 100000 5
1 100000
100000 1
100000 100000
50000 50000
1 50000

出力例 3

499994

Score : 433 pts

Problem Statement

Takahashi is writing a program for a robot that delivers packages in a warehouse.

The warehouse floor is divided into a grid of N rows and M columns. Cell (r, c) denotes the cell in the r-th row from the top and the c-th column from the left (1 \leq r \leq N, 1 \leq c \leq M). There are no obstacles in the warehouse, and all cells can be freely traversed.

There are K delivery destinations (package drop-off points) in the warehouse. The i-th (1 \leq i \leq K) drop-off point is located at cell (R_i, C_i). A drop-off point may exist at cell (1, 1).

The robot starts at the charging station located at cell (1, 1), the top-left corner of the grid. The robot can move to an adjacent cell (up, down, left, or right) in one step. When the robot reaches a cell (including being at that cell initially as the starting position), if there is a drop-off point at that cell, the delivery is automatically completed without any additional steps. The robot may pass through the same cell multiple times.

The robot must visit all drop-off points at least once each to complete all deliveries, and then return to cell (1, 1) for recharging.

Find the minimum total number of steps required to complete all deliveries and return to cell (1, 1).

When K = 0, there are no delivery destinations, so the robot does not need to move, and the answer is 0.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 0 \leq K \leq 15
  • 1 \leq R_i \leq N (1 \leq i \leq K)
  • 1 \leq C_i \leq M (1 \leq i \leq K)
  • (R_i, C_i) \neq (R_j, C_j) (i \neq j)
  • All input values are integers

Input

N M K
R_1 C_1
R_2 C_2
\vdots
R_K C_K
  • The first line contains the number of rows N, the number of columns M, and the number of drop-off points K, separated by spaces.
  • The following K lines each contain, for the i-th line (1 \leq i \leq K), the row number R_i and column number C_i of the i-th drop-off point, separated by spaces.

Output

Print on one line the minimum number of steps required to visit all drop-off points and return to cell (1, 1).


Sample Input 1

3 3 0

Sample Output 1

0

Sample Input 2

5 5 3
1 5
5 1
3 3

Sample Output 2

16

Sample Input 3

100000 100000 5
1 100000
100000 1
100000 100000
50000 50000
1 50000

Sample Output 3

499994