Old Factory Plumbing

Time limit5sMemory limit128 MB

Summary
Choose a water height so the flooded region avoids open holes unless plugged or piped, minimizing pipe distances plus 0.5 per plug.
Level

Hard9 of 10

Topics
Graph, Minimum spanning tree, Greedy, Geometry
Solved
No attempts yet

Problem

You have been hired to build a system that carries water between two points in an old factory, reusing parts of the building's old plumbing. The old plumbing consists of pipes and junctions. A junction is a point where pipes were once joined. Some of the old pipes were damaged and removed, which left open holes in the junctions they used to connect. If water ever reaches an open hole, it pours out and floods the building — an outcome you must avoid.

You can fix this by installing new pipes between open holes and by installing plugs to close other open holes. A new pipe joins two open holes that lie in two different junctions; once it is installed, those two holes are closed and water can flow through the new pipe. The cost of a new pipe equals the Euclidean distance between the centers of the two junctions it connects. The cost of one plug is 0.50.5. You do not care about open holes in junctions that water never reaches.

Two junctions are special: the source (junction 11), where water is pumped in, and the destination (junction NN), where water is needed. After all plugs and new pipes are installed, water is pumped in at the source with a pressure that raises it to a height of your choosing. The pressure is constant and you may choose it freely, but it must be at least large enough to raise water to the heights of both the source and the destination. Your task is to find the cheapest way to bring water from the source to the destination without flooding the building.

Water obeys the following rules. If the pressure is enough to fill a junction, that junction stays filled. From a filled junction, water always flows through pipes that go horizontally or downward, and it flows through an upward pipe only up to the height set by the pressure. Concretely, if the water level you choose is HH, then water fills exactly those junctions whose height is at most HH and that are connected to the source through a pipe path passing only through junctions of height at most HH. If water reaches an open hole in any filled junction, the building floods.

Existing pipes and new pipes never interfere with one another, nor with any junction other than the ones they connect (even if the straight segment between two junctions passes through a third junction, the pipe does not touch it).

Input

The input contains several test cases and ends at end of file.

The first line of each test case contains two integers NN and MM, where NN (2≤N≤4002 \le N \le 400) is the number of junctions (numbered 11 through NN) and MM (0≤M≤500000 \le M \le 50000) is the number of existing usable pipes.

Each of the next NN lines contains four integers xix_i, yiy_i, ziz_i, and kik_i with −10000≤xi,yi,zi≤10000-10000 \le x_i, y_i, z_i \le 10000 and 0≤ki≤4000 \le k_i \le 400. Line ii describes junction ii: (xi,yi,zi)(x_i, y_i, z_i) is its position, where the zz-axis is vertical, and kik_i is the number of open holes in it.

Each of the next MM lines contains two integers aja_j and bjb_j with 1≤aj<bj≤N1 \le a_j < b_j \le N, meaning that pipe jj connects junctions aja_j and bjb_j. At most one pipe connects any pair of junctions, and no two junctions share the same coordinates. The source is junction 11 and the destination is junction NN.

Output

For each test case, output one line in the form Case x: v, where x is the test case number starting from 11. If it is possible to connect the source to the destination without flooding the building, v is the minimum total cost, printed to exactly four decimal places. Otherwise v is the word impossible.

Examples3

  1. Example 1

    Input
    7 6
    2 0 1 1
    0 0 0 2
    1 0 4 3
    3 0 4 3
    5 0 1 1
    3 0 2 0
    5 0 3 0
    1 2
    1 3
    3 4
    4 7
    5 7
    6 7
    4 1
    2 0 0 0
    3 0 1 0
    4 1 0 1
    5 1 1 1
    1 2
    
    Expected output
    Case 1: 4.0000
    Case 2: impossible
    
  2. Example 2

    Input
    2 1
    0 0 0 2
    1 0 0 1
    1 2
    
    Expected output
    Case 1: 1.5000
    
  3. Example 3

    Input
    2 0
    0 0 0 1
    4 0 0 1
    
    Expected output
    Case 1: 4.0000