Given a planar straight-line graph (castle walls) where crossing a wall costs its height, find the minimum cost to travel between successive query points, starting from infinity.
Hard9GraphGeometryShortest pathDFSNo attempts yetTime limit2sMemory limit512 MBTwo thousand years ago the castle of the city state Inho stood on this very spot. The castle has N watchtowers numbered 1 through N and M walls, each wall joining two towers. Every wall is a straight segment, no two walls meet at any point other than a tower, and the walls let you walk from any tower to every other tower. King Inho of Inho hid the state secrets inside that castle.
King Minseok of the rival city state Minseok sends a secret agent in to dig the secrets out. The agent is so fast that travel time is negligible whenever no wall blocks the way. Climbing over a wall costs exactly the height of that wall. The agent cannot pass through a tower, so every crossing happens at a point of a wall that is not a tower.
Minseok orders the agent to visit Q spots in the given order. No spot lies on a wall or on a tower. The agent starts infinitely far away from the castle, goes to spot 1, then to spot 2, and so on. Find the minimum time each leg of the trip takes.
The first line contains the number of towers N (1≤N≤200000) and the number of walls M (N−1≤M≤N+100).
Each of the next N lines contains the coordinates xi and yi of tower i. (∣xi∣,∣yi∣≤109)
Each of the next M lines contains the numbers ui and vi of the two towers a wall joins, followed by the height hi of that wall. (1≤ui,vi≤N, ui=vi, 1≤hi≤109)
At most one wall joins a given pair of towers, and no two walls meet at a point other than a tower. Every tower is reachable from every other tower along the walls.
The next line contains the number of orders Q (1≤Q≤200000).
Each of the next Q lines contains the coordinates ai and bi of the i-th spot. (∣ai∣,∣bi∣≤109)
No spot lies on a wall or on a tower.
Print Q lines. Line i holds the minimum time needed to travel from spot i−1 to spot i. Spot 0 sits at (∞,∞), infinitely far from the castle.
The picture below shows a castle with 4 towers and 5 walls, together with the 3 spots visited in order.
