Nightman
Time limit5sMemory limit1024 MB
Given up to 10 guards, 50 axis-aligned rectangular obstacles, and 10 query points in a rectangle, find the shortest obstacle-avoiding path from each query to its nearest guard and sum the doubled distances.
- Level
Hard8 of 10
- Topics
- Geometry, Graph, Shortest path, BFS
- Solved
- No attempts yet
Problem
Y Center, a sprawling facility, is a state-run complex for training and events. It hosts club meetings and overnight retreats, regular concerts by a civic orchestra, and corporate onboarding programs, so many people use it for many purposes.
As the figure below shows, the grounds of Y Center form a rectangle wide and tall. The lower-left corner has coordinates and the upper-right corner has coordinates .

Figure 1: Y Center with width 8 and height 6 (the case , )
Y Center contains buildings. Every building is a rectangle whose sides are parallel to one of the coordinate axes. The location of a building is given by four integers (). The lower-left corner of building is and its upper-right corner is . The boundary of a building is not part of the building, so even if two buildings touch, there is a gap between them that can be walked through. The building footprints do not overlap.
To keep nighttime visitors safe, guards are placed at coordinates . Every guard is placed outside the buildings.
Late at night, security inside Y Center works as follows. Whenever a suspicious object is found at some point on the grounds, all guards are notified. The guard whose travel distance to that point is shortest rushes to the scene and inspects the object. The guard moves along a shortest path inside Y Center, but cannot pass through the interior of a building because the buildings are locked at night. (The boundary of a building is not part of the building, so a guard may move along a building's edge.) After inspecting the object, the guard returns to their position along the same path. The next time a suspicious object is found, the same procedure applies.

Figure 2: A guard rushing to the scene (○ is a guard, × is a suspicious object)
Late one night, suspicious objects were found outside the buildings inside Y Center. Given the size of Y Center, the positions of the guards, the positions of the buildings, and the positions of the suspicious objects found that night in the order they were discovered, write a program that computes the total travel distance of the guards.
Input
The first line of the input contains the integer , the number of guards (), the integer , the number of buildings (), and the integer , the number of suspicious objects ().
The second line contains two integers (), the width and height of the grounds of Y Center, in that order.
Line () contains two integers (, ), the position of guard . Line () contains four integers (, ), the position of building . Line () contains two integers (, ), the position of suspicious object .
Output
Write the output to standard output.
Print the total travel distance of the guards to three decimal places. The error must be at most 0.001.