Off the Rails
Time limit5sMemory limit512 MB
Given n cities sorted by x, cover them with straight non-vertical segments so that the sum of squared vertical distances plus C per segment is minimized.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Geometry, Math, Greedy
- Solved
- No attempts yet
Problem
The Country of Everlasting is planning a rail line that connects its cities. The route has to keep the distance between the cities and the rail line as small as possible. While canvassing materials, the engineers found that buying pre-fabricated guideways from the country of Forever is the best option. Forever sells straight guideways only. If the chosen route is not a single straight line (as in the figure below), several pre-fabricated guideways are needed. Guideways of different lengths may be bought.

Every pre-fabricated guideway imported from Forever carries an overhead cost of . The route therefore has to minimize , where:
- is the sum of the squares of the lengths of the vertical segments from each city to the rail line.
- is the number of pre-fabricated guideways.
These rules also hold:
- The guideways need not be connected to one another.
- No guideway may be placed vertically.
- No vertical line meets the interiors of two guideways at two different points.
Input
The first line contains , the number of test cases.
The first line of each test case contains an integer and a real number , separated by one space. Each of the next lines describes one city. The th of those lines contains two integers and , the coordinates of the th city.
Constraints
- is given with at most 3 decimal places.
Output
For each test case, print the minimum value of on its own line. Round at the fifth decimal place and print exactly four digits after the decimal point, keeping trailing zeros. A value of is printed as 1.0000.