Dropped Water Bottle
Time limit2sMemory limit512 MB
Joe runs along two river banks with obstacles that force crossings; find the earliest time he intercepts a drifting bottle, or report impossibility.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
Joe is on a multi-day tramping trip. While he is wading across a river, his empty water bottle falls out of his pack and drifts off downstream. He watches it for a while, decides it is worth chasing, returns to the left bank and puts his pack down. Making up his mind and getting back to the left bank takes 2 minutes, and the river flows at a steady metres per second, so the bottle is already metres downstream when he starts to run.
Take the moment Joe starts running as time and the point where the bottle fell as distance . At time seconds the bottle is at metres.
On the left bank Joe runs at metres per second. The right bank is rougher and he runs at metre per second there. Both banks carry obstacles such as cliff faces that he cannot pass. When he meets one he has to cross to the opposite bank. He always crosses on a line perpendicular to the current, so a crossing carries him no distance downstream, and a crossing from one bank to the other takes seconds.
An obstacle given by and covers its own bank from to . Joe can stand at any point of his bank that is not strictly inside an obstacle, and the two end points and are usable. He runs only through such points, and he crosses the river only at a point that is usable on both banks. So if an obstacle on one bank ends exactly where an obstacle on the other bank begins, Joe crosses at that point. If one point lies strictly inside an obstacle on both banks at once, Joe cannot get past it.
To pick the bottle up, Joe wades from the point where he stands into the centre of the river. Reaching the centre takes seconds and carries him no distance downstream, so entering at point at time puts him in the centre of the river at at time . If the bottle has already passed by then, he misses it. If it has not, he waits there and takes it the instant it reaches .
Compute the earliest time at which Joe picks up his water bottle.
Input
The input holds a single test case.
The first line has two integers and , the number of obstacles on the left bank and on the right bank (, ).
Each of the next lines has two integers and describing a left-bank obstacle (, ). Here is the distance downstream from where the bottle was dropped to the start of the obstacle and is the length of the obstacle, both in metres.
The following lines describe the right-bank obstacles in the same format.
On each bank the obstacles are given in increasing order of and do not overlap, though they may touch.
Output
Print the earliest time in seconds at which Joe picks up his bottle, rounded to exactly three digits after the decimal point. The exact value is always an integer multiple of of a second, so the rounded value is unique. For an exact value of seconds, print 66.667.
If Joe cannot pick the bottle up, print IMPOSSIBLE.