This page is still under construction.

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

Watering the Bean Plants

Time limit10sMemory limit256 MB

Summary
Place at most one disk of radius R and cover the uncovered parts of N segments with unit-length sticks at minimum total cost.
Level

Hard9 of 10

Topics
Geometry, Greedy, Brute force
Solved
No attempts yet

Problem

Kyeonggeun gave his bean plants plenty of fertilizer and no water, so the plants rotted. This time he wants equipment that waters all of them.

The bean plants grow along NN line segments in the plane and fill each segment completely. Each segment joins two different points.

Kyeonggeun picks at most one point of the plane and installs a sprinkler there. The sprinkler waters every plant whose distance from that point is at most RR, and it costs C1C_1. He may also install no sprinkler.

Every plant left outside the sprinkler range needs a waterer. One waterer is a straight piece of length exactly 11 and waters only the plants it lies on. It cannot be cut, and one piece costs C2C_2. He places as many pieces as he wants, anywhere and in any direction, and the pieces may overlap or stick out past the end of a segment.

Compute the smallest cost of watering every plant.

Input

The first line has the number of test cases TT.

The first line of each test case has the number of segments NN (1≤N≤501 \le N \le 50), the radius RR of the sprinkler range (0≤R≤150 \le R \le 15), the price C1C_1 of the sprinkler (1≤C1≤10001 \le C_1 \le 1000), and the price C2C_2 of one waterer (1≤C2≤10001 \le C_2 \le 1000).

Each of the next NN lines has the start point xsx_s, ysy_s and the end point xex_e, yey_e of one segment, separated by spaces (0≤xs,ys,xe,ye≤500 \le x_s, y_s, x_e, y_e \le 50). The start point and the end point are different.

No two segments touch or overlap, and the lengths of the segments add up to at most 100100.

Every input value is an integer.

Output

For each test case, print on its own line the smallest cost of watering every plant.

Examples2

  1. Example 1

    Input
    1
    2 1 10 20
    0 0 0 1
    10 0 11 0
    
    Expected output
    30
    
  2. Example 2

    Input
    3
    1 2 5 100
    0 0 0 3
    2 1 1000 1
    0 0 6 0
    0 3 6 3
    2 0 7 9
    0 0 0 2
    5 5 5 8
    
    Expected output
    5
    12
    45