File size: 3,586 Bytes
e329daf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
**N** ladders have been set up in a room, which can be represented as a 2D plane when viewed from the side. The room's floor is the horizontal line with y-coordinate 0, and its ceiling is the horizontal line with y-coordinate **H**. The _i_th ladder is a vertical line segment between integral coordinates (**Xi**, **Ai**) and (**Xi**, **Bi**), located within the inclusive bounds of the room (such that 0 ≤ **Ai** < **Bi** ≤ **H**). Note that each ladder may be touching the floor and/or ceiling, or may be floating in mid-air (don't question it). No two ladders overlap with one another (even at their endpoints).
Sneider the Snake has taken an interest in this room, and may add 0 or more
snakes to it. The _j_th snake will be a vertical line segment between some
coordinates (**xj**, **aj**) and (**xj**, **bj**), located strictly inside the
bounds of the room (with **xj** being any non-negative real number, and **aj**
and **bj** being integers such that 0 < **aj** ≤ **bj** < **H**). A snake may
be a length-0 line segment, with its endpoints being equal, in which case it
occupies only a single point on the plane. No snake may overlap with any other
snake, nor with any ladder (even at an endpoint).
Flynn the Flying Squirrel finds herself on the floor at coordinates (0, 0),
and wants to reach coordinates (0, **H**) on the ceiling. At any point, she
may gracefully hover horizontally (left or right) as long as she doesn't
overlap with any snake (including exactly at one of its endpoints). She may
also move vertically (up or down) as long as she's overlapping with a ladder
(including exactly at one of its endpoints). Flynn always moves continuously
around the plane (she does not skip from one integral coordinate to the next).
Sneider the Snake doesn't want Flynn to reach her destination, just because he
likes being mean. Determine the minimum possible sum of lengths of snakes
which Sneider must place such that Flynn will be unable to reach coordinates
(0, **H**) from her initial position (0, 0), if possible.
### Input
Input begins with an integer **T**, the number of rooms. For each room, there
is first a line containing the space-separated integers **N** and **H**. Then,
**N** lines follow, the _i_th of which contains the space-separated integers
**Xi**, **Ai**, and **Bi**.
### Output
For the _i_th room, print a line containing "Case #_i_: " followed by 1
integer, either the minimum total length of snakes required, or -1 if Sneider
cannot prevent Flynn from reaching coordinates (0, **H**).
### Constraints
1 ≤ **T** ≤ 150
1 ≤ **N** ≤ 50
1 ≤ **H** ≤ 100,000
0 ≤ **Xi** ≤ 100,000
0 ≤ **Ai** < **Bi** ≤ **H**
0 ≤ **xj**
0 < **aj** ≤ **bj** < **H**
### Explanation of Sample
In the first case, one optimal way for Sneider to prevent Flynn from reaching
coordinates (0, 4) involves placing a length-2 snake with endpoints (0.5, 1)
and (0.5, 3), as illustrated below (with ladders indicated in brown and the
snake indicated in green):

In the second case, Flynn already can't reach coordinates (0, 100) even if
Sneider doesn't place any snakes.
In the third case, Flynn cannot be prevented from reaching coordinates (0, 9).
In the fourth case, one optimal way for Sneider to prevent Flynn from reaching
coordinates (0, 30) involves placing a length-1 snake with endpoints (9, 20)
and (9, 21), two length-0 snakes at coordinates (14, 20) and (16, 20), and a
length-2 snake with endpoints (24, 20) and (24, 22). The sum of these snakes'
lengths is 1 + 0 + 0 + 2 = 3.
|