eolymp
bolt
Try our new interface for solving problems

Bug2

There is a variety of navigation problems around us. This is a problem about an algorithm called \textit{Bug2}. \textit{Bug algorithms} solve the following navigation problem. There is a two-dimensional map containing obstacles of an arbitrary shape, and start and finish points are given. There is also an "agent", who initially stands at the start point \textbf{S}, and its task is to reach the finish point \textbf{F}. It knows the coordinates of the finish point, and at any moment of time it can determine its own coordinates. The agent has \textbf{O(1)} memory, so it cannot store the map of obstacles. The only way it can get information about the outer world is to detect whether it touches an obstacle. The Agent is able to move around the obstacle following its boundary. The problem is to reach the finish point when it is possible, and correctly report the fact of unreachability otherwise. The \textbf{Bug2} algorithm, which belongs to the family of Bug algorithms, works as follows: 1. Move towards \textbf{F} until one of the following happens: \begin{itemize} \item The finish point is reached. Then the algorithm stops. \item An obstacle is encountered. Then go to step \textbf{2}. \end{itemize} \textbf{2}. Define the current point as \textbf{H}. Move around the boundary of the obstacle in the clockwise direction until one of the following happens: \begin{itemize} \item The finish point is reached. Then the algorithm stops. \item The point \textbf{H} is reached. Then the finish point is unreachable, and the algorithm stops. \item A point \textbf{L} is reached, which lies on the line \textbf{SF}, \textbf{|LF|} < \textbf{|HF|} and it is possible to move towards \textbf{F} without hitting an obstacle immediately. In this case, go to step \textbf{1}. \end{itemize} \includegraphics{https://static.e-olymp.com/content/cd/cda7c02c89b054e054c4142163478d006d73fe04.jpg} One may prove the correctness of an algorithm, that is, that it reaches the finish point in finite time (that is, the length of the resulting path is finite) if it is possible, and reports that the finish point is unreachable in finite time otherwise. Given a set of polygonal obstacles, a start and a finish point, determine the length of the path that an agent directed by \textit{Bug2} algorithm will traverse. \InputFile The first line of the input file contains five integer numbers \textbf{n}, \textbf{x_S}, \textbf{y_S}, \textbf{x_F}, \textbf{y_F} - the number of obstacles and the coordinates of start and finish points. The rest of the input file describes obstacles. Each description starts with a line containing an integer \textbf{m} (\textbf{m} ≥ \textbf{3}) - the number of vertices in the obstacle. The following \textbf{m} lines contain pairs of integer numbers \textbf{x_i}, \textbf{y_i }- the coordinates of vertices of the obstacle, given in the clockwise direction. The obstacle does not have self-intersections or self-tangencies. The total number of vertices in all the obstacles does not exceed \textbf{300000}. No coordinate exceeds \textbf{10^6} by an absolute value. No vertex of an obstacle lies on a line \textbf{SF}. Both start and finish point will lie strictly outside any obstacle. No two obstacles share common points. If there are two points \textbf{A} and \textbf{B} where obstacle boundaries intersect with the line \textbf{SF}, either \textbf{|AF| = |BF|} or \textbf{||AF| - |BF||} > \textbf{10^\{-6\}} will be true. \OutputFile Output the length of a path traversed by the agent directed by the described algorithm. The absolute or relative error of \textbf{10^\{-6\}} is acceptable.
Time limit 3 seconds
Memory limit 256 MiB
Input example #1
1 0 2 6 2
8
1 0
1 3
2 3
2 1
4 1
4 3
5 3
5 0
Output example #1
10.0
Author Maxim Buzdalov