H - Mirror
Basic Information
Contest | The 2018 ICPC Asia Qingdao Regional Contest |
Team AC Ratio | 0/373 (0.0%) |
Tutorial
We first deal with some special cases.
- If the segment connecting the start and end points has no obstacles, the answer is \((2m - 1) \times l\), where \(l\) is the distance between the start and end points.
- Otherwise, if \(m = 1\), we only need to consider the shortest path from the start to the end, avoiding obstacles and mirrors, no need to consider whether the path can see the start or end points. Therefore, we only need to consider the graph consisting of these seven points: start, end, obstacle vertices, and mirror endpoints, and compute the shortest path from the start to the end.
- Otherwise, \(m > 1\). This requires the existence of a route from the start to the end that can see both the start and end points at any point on the route. If either the start or end point is to the left of the mirror, without loss of generality let's assume that the end point is to the left of the mirror, then it will be impossible to see the start point from the end point, because the segment connecting the start and end points has an obstacle, and the end point cannot see the mirror. Therefore, this case has no solution.
After excluding the above special cases, the remaining case is: \(m > 1\), there are obstacles on the segment connecting the start and end points, and both points are to the right of the mirror.
At this point, the optimal route will be divided into three parts:
- At the beginning, we move the first stone from the start to the end, through a route where the start point can be seen at any time.
- In the middle, we need a route which is able to see the start and end points at any time, and this path will be repeated \((2m-3)\) times.
- Finally, we move the last stone from the start to the end, through a route where the end point can be seen at any time.
Therefore, we need to consider which lines, as they are crossed, may change the visibility of the start and end points. A graph is built based on the intersection points of these lines. We need to calculate the length of the segment between any two points, whether there are obstacles on the segment, and whether each point on the segment can see the start and end points. Finally, the shortest path from the start to the end is calculated under specific visibility requirements.
The lines that cause changes in visibility are:
- The line connecting the start/end point and the obstacle vertex.
- The line connecting the start/end point and the mirror endpoint.
- The line connecting the mirror image of the start/end point and the mirror endpoint.
- The line connecting the mirror image of the start/end point and the obstacle vertex.
- The line connecting the mirror image of the start/end point and the mirror image of the obstacle vertex.
You may think that the boundary of the obstacle and the mirror will also change the visibility, why not consider them? Because a valid path cannot cross the boundary of the obstacle or mirror, there is no need to consider them.
When calculating whether each point on the segment between two points can always see the start and end points, since the segment may cross multiple lines that change the visibility, it is necessary to find the intersection points of the segment with each line and check the visibility of the midpoint of adjacent intersection points. This is correct because the region enclosed by the visibility lines is convex.
There is also an optimization for this problem: in fact, the optimal route is always on the right side of the mirror, so all points on the mirror line and to the left of it can be excluded, and only the points on the right side of the mirror need to be considered. We now roughly calculate the number of points we need to consider. Notice that the \(1\)-st type and the \(5\)-th type of lines are symmetrical, also the \(2\)-nd type and the \(3\)-rd type of lines are symmetrical. So the number of intersections of these lines on the right of the mirror is \(5 \times (15 + 10 + 5) \div 2 = 75\). Even if the intersections of the \(4\)-th type of lines are all on the right of mirror, the number of points to consider will be at most \(75 + 3 \times 18 + 3 \times 10 = 159\). Adding the start and end points, there will be \(161\) points.
So the time complexity is \(\mathcal{O}(n^2t + n^2\log n)\), where \(n = 161\) is the number of points to consider, and \(t = 26\) is the number of lines to change visibility.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|