Secret Agent
Time limit2sMemory limit512 MB
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.
- Level
Hard9 of 10
- Topics
- Graph, Geometry, Shortest path, DFS
- Solved
- No attempts yet
Problem
Two thousand years ago the castle of the city state Inho stood on this very spot. The castle has watchtowers numbered through and 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 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 , then to spot , and so on. Find the minimum time each leg of the trip takes.
Input
The first line contains the number of towers () and the number of walls ().
Each of the next lines contains the coordinates and of tower . ()
Each of the next lines contains the numbers and of the two towers a wall joins, followed by the height of that wall. (, , )
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 ().
Each of the next lines contains the coordinates and of the -th spot. ()
No spot lies on a wall or on a tower.
Output
Print lines. Line holds the minimum time needed to travel from spot to spot . Spot sits at , infinitely far from the castle.
Hint
The picture below shows a castle with 4 towers and 5 walls, together with the 3 spots visited in order.
