#P16624. [GKS 2017 #D] Sightseeing

[GKS 2017 #D] Sightseeing

Problem Description

When you travel, you like to spend time sightseeing in as many cities as possible, but sometimes you might not be able to because you need to catch the bus to the next city. To maximize your travel enjoyment, you decide to write a program to optimize your schedule.

You begin at city 11 at time 00 and plan to travel to cities 22 to NN in ascending order, visiting every city. There is a bus service from every city i to the next city i + 11. The i-th bus service runs on a schedule that is specified by 33 integers: SiS_i, FiF_i and DiD_i, the start time, frequency and ride duration. Formally, this means that there is a bus leaving from city i at all times Si+xFiS_i + xF_i, where x is an integer and x0x \ge 0, and the bus takes DiD_i time to reach city i + 11.

At each city between 11 and N1N - 1, inclusive, you can decide to spend TsT_s time sightseeing before waiting for the next bus, or you can immediately wait for the next bus. You cannot go sightseeing multiple times in the same city. You may assume that boarding and leaving buses takes no time. You must arrive at city NN by time TfT_f at the latest. (Note that you cannot go sightseeing in city NN, even if you arrive early. There's nothing to see there!)

What is the maximum number of cities you can go sightseeing in?

Input Format

The input starts with one line containing one integer TT, which is the number of test cases. TT test cases follow.

Each test case begins with a line containing 33 integers, NN, TsT_s and TfT_f, representing the number of cities, the time taken for sightseeing in any city, and the latest time you can arrive in city NN.

This is followed by N1N - 1 lines. On the i-th line, there are 33 integers, SiS_i, FiF_i and DiD_i, indicating the start time, frequency, and duration of buses travelling from city i to city i + 11.

Output Format

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the maximum number of cities you can go sightseeing in such that you can still arrive at city NN by time TfT_f at the latest. If it is impossible to arrive at city NN by time TfT_f, output Case #x: IMPOSSIBLE.

4
4 3 12
3 2 1
6 2 2
1 3 2
3 2 30
1 2 27
3 2 1
4 1 11
2 1 2
4 1 5
8 2 2
5 10 5000
14 27 31
27 11 44
30 8 20
2000 4000 3
Case #1: 2
Case #2: 0
Case #3: IMPOSSIBLE
Case #4: 4

Hint

In the first test case, you can go sightseeing in city 11, catching the bus leaving at time 33 and arriving at time 44. You can go sightseeing in city 22, leaving on the bus at time 88. When you arrive in city 33 at time 1010 you immediately board the next bus and arrive in city 44 just in time at time 1212.

Limits

1T1001 \le T \le 100.

Small dataset (Test set 11 - Visible)

2N162 \le N \le 16.

1Si50001 \le S_i \le 5000.

1Fi50001 \le F_i \le 5000.

1Di50001 \le D_i \le 5000.

1Ts50001 \le T_s \le 5000.

1Tf50001 \le T_f \le 5000.

Large dataset (Test set 22 - Hidden)

2N20002 \le N \le 2000.

1Si1091 \le S_i \le 10^9.

1Fi1091 \le F_i \le 10^9.

1Di1091 \le D_i \le 10^9.

1Ts1091 \le T_s \le 10^9.

1Tf1091 \le T_f \le 10^9.