GCPC11H - Sightseeing

no tags 

As a computer science student you are of course very outdoorsie, so you decided to go hiking. For your vacation this year, you located an island full of nice places to visit. You already identified a number of very promising tracks, but are still left with some problems. The number of choices is so overwhelming, that you had to select only a "small" subset of at most 105 sights.

And if that is not enough, you are very picky about the order in which you want to visit the sights. So you have already decided on an order in which you want to visit the preselected tracks. The problem you are left with is to decide in which direction to travel along each single track, and whether you may have to reduce your choice of tracks even further. After identifying the travel time between the endpoints of different tracks, you decide to write a program to figure out if you can make all your trips within the time you have planned for your vacation. Since you also do not want to waste any precious time, you only care about an optimal solution to your problem. Furthermore, the tracks can get pretty challenging. Thats why you do not want to hike along a track more than once.

Input

The first line of the input gives the number of test cases C (0 < C ≤ 100). The first line of each such test case holds two integers N, T the number of tracks of the current hiker (1 ≤ N ≤ 105) and the maximal time spent hiking throughout the vacation (0 ≤ T ≤ 106). Each of the following N lines holds five integers cp, cbb, cbe, ceb and cee that describe a track (in order of importance). cp gives the length of the track in minutes. cxy gives the travel time of the official begin or end of a track to the beginning or end of the next most important track, where x and y are either b or e. All values given are non-negative integers not greater than 106. Since you have to get back to your car, the list is circular. Furthermore, we will ignore the time it takes you to get to the start of your trip with your car.

Output

For each test case print one line. The output should contain a list of either F or B for every track (in order) indicating whether you have to hike the track in forward direction or backward direction. If you cannot make the full trip within the planned time T, you should print IMPOSSIBLE to indicate that these trips are just too much hiking. You can assume that the optimal solution is always unique.

Example

Input:
3
2 100
4 7 8 2 3
1 4 6 1 2
2 20
4 2 3 7 8
1 1 2 4 6
3 5
1 2 2 2 1
1 1 2 2 2
1 2 2 1 2

Output:
FF
BB
IMPOSSIBLE

hide comments
sandeep reddy biddala: 2011-07-07 11:13:44

@Adrien Kuegel: thank you for going through my code. could please clarify what forward and backward directions mean and is it necessary to hike through the track or is it enough if we just cover the beginning and end points

Adrian Kuegel: 2011-07-06 19:50:04

@srb:
You are given a graph with 2*n nodes. Node 2 * i is connected to nodes 2 * i - 1, 2 * i + 1, 2 * i + 2, 2 * i - 2 and 2 * i + 3 (with wrap-around). Node 2 * i + 1 is connected to node 2 * i, 2 * i - 1, 2 * i + 2, 2 * i - 2 and 2 * i + 3. What is the cycle with minimum length that will visit each node exactly once and will use the edges from node 2*i to 2*i+1 or 2*i+1 to 2*i for all 0 <= i < n?
Not sure if it is easier to understand like this.
Btw, I looked at your code, and it seems you did understand the problem, but your algorithm is wrong (hint: check your output sequence if it really gives the length you calculate).
edit: I changed 1 <= i <= n into 0 <= i < n

Last edit: 2011-07-07 11:38:22
sandeep reddy biddala: 2011-07-06 17:46:07

could someone please explain the problem in a easy way. i find it difficult to understand.

Adrian Kuegel: 2011-07-06 11:36:51

you should arrive at the same point where the journey started, so that you would be able to hike the first track again in the same direction.

sandeep reddy biddala: 2011-07-06 11:36:51

should the directions of first and last tracks be same as the list is circular.


Added by:Adrian Kuegel
Date:2011-07-05
Time limit:3.295s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:German Collegiate Programming Contest 2011 (Author: Moritz Kobitzsch)