Supply Mission

Time limit2sMemory limit128 MB

Summary
Find the minimum total time for a helicopter to visit every moving submarine in any order, land one hour at each, and return to base.
Level

Hard8 of 10

Topics
Brute force, Geometry, Math, Implementation
Solved
No attempts yet

Problem

You must pilot a helicopter to deliver supplies to several submarines moving across the ocean.

You are given the coordinates of the helicopter base and of each submarine. Submarine ii moves at the constant velocity given by the vector (vx,vy)(v_x, v_y): after one hour it has moved vxv_x km along the x-axis and vyv_y km along the y-axis (vxv_x and vyv_y may be negative). The length of this vector is the submarine's speed.

The helicopter moves at a constant speed in any direction (assume that acceleration and deceleration are instantaneous). It must land on each submarine at least once, and every stop takes exactly one hour to unload supplies and refuel. A submarine surfaces at its scheduled landing time and submerges again once the helicopter leaves; a submarine's velocity is never affected by its depth. While the helicopter waits during that one-hour stop it stays on the submarine, so it moves together with the submarine at the submarine's constant velocity. The helicopter can carry enough supplies for every submarine at once, so it never has to go back to the base during the mission.

All coordinates are in km and all speeds are in km/h.

Find the shortest possible time for the helicopter to start at the base, deliver supplies to every submarine, and return to the base.

Input

The input contains several test cases.

Each test case begins with a line containing an integer NN (1≤N≤81 \le N \le 8), the number of submarines. Each of the next NN lines contains four integers separated by spaces: the initial coordinates xx and yy of that submarine followed by its velocity components vxv_x and vyv_y. The final line of the test case contains three integers: the xx and yy coordinates of the helicopter base and the helicopter's speed.

The input ends with a test case whose first line is N=0N = 0; do not process that case.

Every integer in the input has absolute value at most 10001000. The helicopter is always strictly faster than every submarine. The paths of the submarines may cross one another or even pass through the base, but because they can change depth there are never any collisions.

Output

For each test case, print its case number followed by the minimum time required to complete the mission, in the format:

Case a: b hour(s) c minute(s) d second(s)

where aa, bb, cc, and dd are non-negative integers and cc and dd are at most 5959. Round the time up to the next whole second.

Examples3

  1. Example 1

    Input
    5
    1 0 0 0
    2 0 0 0
    3 0 0 0
    4 0 0 0
    5 0 0 0
    0 0 1
    3
    1 2 3 4
    2 2 40 23
    7 8 22 10
    0 0 50
    0
    
    Expected output
    Case 1: 15 hour(s) 0 minute(s) 0 second(s)
    Case 2: 5 hour(s) 59 minute(s) 50 second(s)
    
  2. Example 2

    Input
    1
    10 0 0 0
    0 0 1
    0
    
    Expected output
    Case 1: 21 hour(s) 0 minute(s) 0 second(s)
    
  3. Example 3

    Input
    1
    0 0 3 4
    10 0 10
    0
    
    Expected output
    Case 1: 2 hour(s) 40 minute(s) 50 second(s)