Aqueduct Construction
Time limit3sMemory limit256 MB
Each town must connect to a distinct spring through downhill hops of limited length so the combined aqueduct length is minimal.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Dynamic programming
- Solved
- No attempts yet
Problem
After conquering Britannia, the Roman general Agricola decided that every one of his new towns should draw water from the natural springs of the province. His advisor Wessus Waterus was told to design the aqueducts.
Hills and valleys lie between the springs and the towns. An aqueduct is built from segments, and every segment starts on one hilltop and ends on another. Water flows downhill only, so a segment must run from a higher hill to a lower one. Two hills of equal height cannot be joined by a segment. A hill, a spring or a town standing under a segment is no problem, because the segment tunnels straight through it. Roman engineering has one limit: a single segment can be at most long.
The length of a segment is the three dimensional Euclidean distance between the two hilltops, . The length of an aqueduct from a spring to a town is the sum of the lengths of its segments.
Every town has to be fed by its own spring, and one spring cannot feed two towns. Aqueducts may cross each other. Make the total length of all the aqueducts as small as possible.
Input
One line with four integers , , and : the number of hills (), the number of springs (), the number of towns () and the maximum length of one segment ().
Then lines. The th of them holds the space separated integers , and , the coordinates and the height of a hill (). The hills are numbered 1 to in the order given.
Then one line with space separated integers, the numbers of the hills that carry a spring.
Then one line with space separated integers, the numbers of the hills that carry a town.
A hill carries at most one spring or one town.
Output
Print one line with the smallest total aqueduct length that feeds every town from a spring of its own, rounded to exactly six digits after the decimal point. If no such supply exists, print IMPOSSIBLE instead.