|
<p> |
|
<strong>N</strong> 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 <strong>H</strong>. |
|
The <em>i</em>th ladder is a vertical line segment between integral coordinates |
|
(<strong>X<sub>i</sub></strong>, <strong>A<sub>i</sub></strong>) and (<strong>X<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>), located within the inclusive bounds of the room |
|
(such that 0 ≤ <strong>A<sub>i</sub></strong> < <strong>B<sub>i</sub></strong> ≤ <strong>H</strong>). |
|
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). |
|
</p> |
|
|
|
<p> |
|
Sneider the Snake has taken an interest in this room, and may add 0 or more snakes to it. The <em>j</em>th snake will be a vertical line segment between some coordinates |
|
(<strong>x<sub>j</sub></strong>, <strong>a<sub>j</sub></strong>) and (<strong>x<sub>j</sub></strong>, <strong>b<sub>j</sub></strong>), located strictly inside the bounds of the room |
|
(with <strong>x<sub>j</sub></strong> being any non-negative real number, and <strong>a<sub>j</sub></strong> and <strong>b<sub>j</sub></strong> being integers such that |
|
0 < <strong>a<sub>j</sub></strong> ≤ <strong>b<sub>j</sub></strong> < <strong>H</strong>). |
|
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). |
|
</p> |
|
|
|
<p> |
|
Flynn the Flying Squirrel finds herself on the floor at coordinates (0, 0), and wants to reach coordinates (0, <strong>H</strong>) 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). |
|
</p> |
|
|
|
<p> |
|
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, <strong>H</strong>) from her initial position (0, 0), if possible. |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of rooms. |
|
For each room, there is first a line containing the space-separated integers <strong>N</strong> and <strong>H</strong>. |
|
Then, <strong>N</strong> lines follow, the <em>i</em>th of which contains the space-separated integers |
|
<strong>X<sub>i</sub></strong>, <strong>A<sub>i</sub></strong>, and <strong>B<sub>i</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th room, print a line containing "Case #<em>i</em>: " |
|
followed by 1 integer, either the minimum total length of snakes required, or -1 if Sneider cannot prevent Flynn from reaching coordinates (0, <strong>H</strong>). |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 150 <br /> |
|
1 ≤ <strong>N</strong> ≤ 50 <br /> |
|
1 ≤ <strong>H</strong> ≤ 100,000 <br /> |
|
0 ≤ <strong>X<sub>i</sub></strong> ≤ 100,000 <br /> |
|
0 ≤ <strong>A<sub>i</sub></strong> < <strong>B<sub>i</sub></strong> ≤ <strong>H</strong> <br /> |
|
0 ≤ <strong>x<sub>j</sub></strong> <br /> |
|
0 < <strong>a<sub>j</sub></strong> ≤ <strong>b<sub>j</sub></strong> < <strong>H</strong> <br /> |
|
</p> |
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
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): |
|
</p> |
|
|
|
<img src={{PHOTO_ID:262526658147947}} /> |
|
|
|
<p> |
|
In the second case, Flynn already can't reach coordinates (0, 100) even if Sneider doesn't place any snakes. |
|
</p> |
|
|
|
<p> |
|
In the third case, Flynn cannot be prevented from reaching coordinates (0, 9). |
|
</p> |
|
|
|
<p> |
|
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. |
|
</p> |
|
|