#P3. [Sleeping Cup #1] Moving like a spider

[Sleeping Cup #1] Moving like a spider

Person in Charge

Attention

This problem requires file I/O (spider.in / spider.out).

Problem Description

An image is available here. If not visible, download it from the "Attachments" section.

As shown in the image, this is a spider web with the following elements:

  • Central point OO, marked in yellow on the left side of the figure;
  • nn rays emanating from OO (only 44 are drawn in the figure), labeled as lines x=0x=0, x=1x=1, x=2x=2, ..., x=n1x=n-1;
  • Multiple concentric circles centered at OO (only 33 are drawn in the figure, with partial arcs shown), labeled as circles y=1y=1, y=2y=2, y=3y=3, ...;
  • Intersection points formed by the circles and rays (excluding OO). The intersection of circle y=y0y=y_0 and ray x=x0x=x_0 is denoted as (x0,y0)(x_0,y_0). For example, the top-right intersection in the figure is (2,3)(2,3).

There are mm spiders moving on this web, with their initial positions given (guaranteed not to be OO, so they can all be represented by coordinates).

Each spider can move multiple times (the number of moves is up to you, including 0 moves). Generally, there are 44 possible directions for each move. When starting from (x0,y0)(x_0,y_0):

Direction Visualization Destination Energy Cost Special Cases
Up Move along the ray away from OO (x0,y0+1)(x_0,y_0+1) 00 (1)
Down Move along the ray toward OO (x0,y01)(x_0,y_0-1) dd (2)
Left Move clockwise along the circle (x01,y0)(x_0-1,y_0) ry0ry_0 (3)
Right Move counterclockwise along the circle (x0+1,y0)(x_0+1,y_0) (4)

The special cases (last column) are:

  1. If the starting point is OO (in which case the only possible move is "up"), the destination can be any of (0,1),(1,1),...,(n1,1)(0,1),(1,1),...,(n-1,1), chosen by you.
  2. If y0=1y_0=1, the destination will be OO.
  3. If x0=0x_0=0, the destination will be (n1,y0)(n-1,y_0).
  4. If x0=n1x_0=n-1, the destination will be (0,y0)(0,y_0).

Now, these mm spiders need to gather at a meeting point (either OO or an intersection point). Your task is to choose an optimal meeting point and plan the movement paths for each spider to minimize the total energy cost, then output this minimum total energy.

Input Format

First line: Four positive integers, m,n,d,rm,n,d,r.

Next mm lines: The ii-th line contains two integers xi,yix_i,y_i, representing the initial position (xi,yi)(x_i,y_i) of the ii-th spider.

Output Format

One integer: The answer.

Samples

3 7 9 8
0 2
6 4
0 5
32
2 2 3 5
0 1
1 1
3
7 11 63599913 20656382
9 9
1 11
6 4
0 4
1 15
2 11
6 20
2393965956

Explanation for Sample 1

An optimal solution is to choose (0,5)(0,5) as the meeting point:

  • Spider 11 moves along (0,2)(0,3)(0,4)(0,5)(0,2) \to (0,3) \to (0,4) \to (0,5) (3 "up" moves), costing 0+0+0=00+0+0=0;
  • Spider 22 moves along (6,4)(0,4)(0,5)(6,4) \to (0,4) \to (0,5) (1 "right" move and 1 "up" move), costing 8×4+0=328 \times 4 + 0 = 32;
  • Spider 33 stays in place, costing 00.

The total cost is 0+32+0=320+32+0=32.

Explanation for Sample 2

An optimal solution is to choose (1,1)(1,1) as the meeting point:

  • Spider 11 moves along (0,1)O(1,1)(0,1) \to O \to (1,1) (1 "down" move and 1 "up" move), costing 3+0=33+0=3;
  • Spider 22 stays in place, costing 00.

The total cost is 3+0=33+0=3.

Data Range

For 100%100\% of the data: 2m,n1052 \le m,n \le 10^5, 1yi1051 \le y_i \le 10^5, 1d,r1081 \le d,r \le 10^8, 0xin10 \le x_i \le n-1.

Attachments

Download the files here.

Official Solution

link