Color Tunnels
Time limit1sMemory limit128 MB
Given a color sequence and colored line-segment tunnels, find the shortest path from source to destination that traverses tunnels in the required color order.
- Level
Hard8 of 10
- Topics
- Geometry, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
A toy factory paints its products by moving them through colored tunnels. To reach a desired final color, a product must be painted by an ordered sequence of colors.
Each tunnel is a straight line segment of a fixed color, placed somewhere in the plane. A single color may be produced by several tunnels, and different tunnels may share the same color. To be painted with a color, the product must travel through a tunnel of that color from one endpoint to the other (a full traversal); the direction of travel does not matter.
Formally, an unpainted product starts at a given source point and must be delivered to a given destination point after being painted, in order, with the colors . The product must therefore traverse tunnels (in this order) such that tunnel has color . It may also pass through or cross other tunnels along the way; only the chosen subsequence has to match the required colors. Between traversals the product moves along straight line segments through the obstacle-free plane. The path may cross itself or merely intersect a tunnel without traversing it (which does not count as painting), and the same tunnel may be traversed more than once.
Compute the length of the shortest such path from the source to the destination.
Input
The first line contains an integer (), the number of test cases. Each test case is given as follows.
- One line with four real numbers : the coordinates of the source and of the destination .
- One line describing the color sequence: an integer () giving its length, followed by integers (each in ), the required colors in order.
- One line with an integer (), the number of tunnels.
- lines, each with five numbers : the endpoints and (real numbers) and the color (an integer in ) of one tunnel.
Every color that appears in a sequence is produced by at least one tunnel, so a valid path always exists.
Output
For each test case, print a single line containing the minimum total travel length from the source to the destination that traverses tunnels matching the required color order, rounded to exactly three digits after the decimal point.