루미 알고리즘

#1587플래티넘Fire Hydrant

PO 2024 온라인 예선코딩출제 Erik Hedin, Harry Zhang영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Firefighter Robert has been working as a firefighter for over 20 years and loves his job. After a long walk in the metropolis M (formerly known as Stockholm), Robert received a call on his phone from his hydrophobic chief, ordering him to come to the fire station right away. Robert has just arrived at the most south-west four-way intersection in metropolis M and decides to head straight to the fire station, but apparently, several fire hydrants in metropolis M have started leaking water and flooding the streets! Oh no! This is particularly serious because Robert's hydrophobic chief at the fire station is extremely strict about not having water near him, so showing up soaking wet to work after encountering several of the leaking fire hydrants will get Robert fired. Robert wants to work as a firefighter for at least 20 more years, so help Robert minimize how wet he gets!

Metropolis M consists of a grid with dimensions W × H, where each four-way intersection is a square in the grid, and is reachable from the four adjacent intersections in each cardinal direction: north, east, south, and west. Robert has just arrived at the four-way intersection furthest southwest in the grid, at position (1,1). Robert wants to reach the fire station furthest northeast in the grid, at position (W,H). It takes exactly 1 minute to run from one four-way intersection to another adjacent intersection. It is guaranteed that the fire station is not located at (1,1).

Furthermore, there are N four-way intersections with a leaking fire hydrant, which can be located in any square on the grid, including where Robert starts from (1,1) and at the fire station (W,H). Robert arrives at the square (1,1) at minute 0, and each leaking fire hydrant has leaked one unit of water to the intersection the fire hydrant is located at. For each minute t that passes, each leaking fire hydrant will leak an additional unit of water to all intersections within a certain distance from it. Specifically, all squares (x,y) that satisfy the following inequality at minute t will increase by one unit of water from the fire hydrant located at (x_b,y_b):

|x-x_b|+|y-y_b| ≤ t.

Let's call the number of units of water at a particular square v_t at minute t. When Robert arrives at a four-way intersection at minute t, Robert's clothes will get wet from all the water present at that intersection from leaking fire hydrants, which is v_t units of water.

What is the minimum total number of water units that Robert needs to go through on his way to the fire station?

(그림은 원문 저장소에서 볼 수 있습니다)

Figure 1: Illustration of sample 1, where the green square is Roberts position and the red squares are the positions of the fire hydrants. The smallest total water units Bob can have when arriving at the station is 0+0+2+2+4+6+8+8=30.

Input

The first line of the input contains three integers W,H,N (1 ≤ W,H ≤ 1000, 1 ≤ N ≤ min(20000,W · H)), where W and H are the width and height of the grid and N is the number of leaking fire hydrants.

The following N rows each contain two integers x_i and y_i (1 ≤ x_i ≤ W, 1 ≤ y_i ≤ H), where (x_i,y_i) is the position of the i:th leaking fire hydrant. It is guaranteed that no two fire hydrants occupy the same cell.

Output

Print an integer, the minimum number of water units Robert has to pass through on his way to the fire station.

Points

Your solution will be tested on several test case groups. To get the points for a group, it must pass all the test cases in the group.

GroupPoint valueConstraints
110W,H ≤ 10, N=1 and the only fire hydrant is located at (1,1).
215W,H,N ≤ 50
317W,H ≤ 100, N ≤ 200
427W,H ≤ 150
531No additional constraints.

예제

예제 1

입력
5 4 2
4 1
2 3
출력
30

예제 2

입력
2 2 1
2 2
출력
4

출처 표기

출처: Programmeringsolympiaden 2024년 온라인 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.