This page is still under construction.

Parts of this page are still being built. What you see may change.

Optimal Space Way

Time limit5sMemory limit128 MB

Summary
For each test case, fit a weighted straight line through the plane minimizing the mean squared perpendicular distance from given points, and answer queries that give one point extra weight.
Level

Medium7 of 10

Topics
Geometry, Math, Prefix sum, Sorting
Solved
No attempts yet

Problem

By the year 2180 AD, humanity has begun to leave Earth and settle in space. Thousands of space cities are built on a single imaginary plane so that they can all share one universal business hour, so the location of every city is a point (x,y)(x, y) on a two-dimensional Cartesian plane.

The cities lie far apart, and the only way to travel between them is by shuttle rocket. Because rockets are expensive, the space agency decides to build a single super space-way (SSW): one perfectly straight road that may extend without bound in both directions.

To travel from a city c1c_1 to a city c2c_2, a traveler flies by rocket from c1c_1 to the point of the SSW nearest to c1c_1, moves cheaply along the SSW, and then flies by rocket from the point of the SSW nearest to c2c_2 over to c2c_2. The nearest point of a straight line to a city is the foot of the perpendicular from the city to the line, so the rocket distance for a city equals its perpendicular distance to the SSW.

The cost of a rocket that flies a distance vv is v2v^2; travel along the SSW is comparatively free and is ignored. Every ordinary city sends and receives the same number of rocket flights per calendar year. You must place the SSW so that the total yearly rocket cost is as small as possible, and report the resulting minimum average cost per rocket flight.

Because every ordinary city has the same number of flights, the average cost per rocket flight is exactly the average of the squared perpendicular distances from the cities to the SSW, and you may choose the line that minimizes this average.

Sometimes exactly one city is marked as the super city, the center of all activity. A super city has MM times as many rocket flights (incoming plus outgoing) as an ordinary city, while every other city stays ordinary. In that case each city's squared perpendicular distance is weighted by its number of flights, and you report the minimum weighted average cost per rocket flight (the minimum of ∑iwi di2∑iwi\dfrac{\sum_i w_i\, d_i^2}{\sum_i w_i} over all lines, where did_i is the perpendicular distance of city ii to the line, wi=Mw_i = M for the super city, and wi=1w_i = 1 for every ordinary city).

Assumptions: the cities are points; the SSW is an infinitely thin straight line whose length may grow without bound in either direction whenever that lowers the cost; and every rocket flies in a straight line.

Input

The input contains fewer than 5050 test cases.

Each test case begins with two integers NN and QQ (0<N≤100000 < N \le 10000, 0<Q≤1000 < Q \le 100), where NN is the number of space cities and QQ is the number of queries. Each of the next NN lines contains two floating-point numbers xix_i and yiy_i (0.0≤xi,yi≤1000.00.0 \le x_i, y_i \le 1000.0), the coordinates of the ii-th city. Cities are numbered from 00 to N−1N-1 in the order they appear. Each of the next QQ lines contains two integers SS and MM (0≤S≤N−10 \le S \le N-1, 1<M≤100001 < M \le 10000): city SS is the super city, and its total number of rocket flights is MM times that of an ordinary city.

A line containing two zeros terminates the input and must not be processed.

Output

For each test case, print Q+2Q + 2 lines.

  • The first line is Case k:, where kk is the test-case number starting from 11.
  • The second line is the minimum average cost per rocket flight when every city is ordinary.
  • Each of the next QQ lines corresponds to one query, in the given order, and has the form i: value, where ii is the query number starting from 11 and value is the minimum average cost per rocket flight when city SS of that query is the super city and every other city is ordinary.

Every cost must be printed with exactly five digits after the decimal point.

Examples2

  1. Example 1

    Input
    5 2
    464.9900 243.2652
    463.9409 772.4632
    201.9822 561.6255
    695.8948 933.4567
    226.0628 93.1435
    3 2
    4 3
    4 2
    27.1679 304.2512
    27.7639 16.2479
    921.9150 863.0064
    167.6203 929.5471
    2 2
    2 3
    0 0
    
    Expected output
    Case 1:
    16172.49971
    1: 14289.23473
    2: 11558.37654
    Case 2:
    53198.72595
    1: 47995.33546
    2: 41543.27604
    
  2. Example 2

    Input
    4 2
    0.0000 0.0000
    0.0000 10.0000
    10.0000 0.0000
    10.0000 10.0000
    0 3
    2 4
    0 0
    
    Expected output
    Case 1:
    25.00000
    1: 16.66667
    2: 14.28571