Teleport
Time limit2sMemory limit512 MB
Given N cities with coordinates, some special, travel costs Manhattan distance; special cities also connect by teleport costing T. Answer M shortest-path queries.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Math, Implementation
- Solved
- No attempts yet
Problem
There are cities on a two-dimensional plane. Some of the cities are special. The travel time from the city at to the city at is . If both cities are special, you may also travel between them by teleport. A teleport takes time.
Given pairs of cities, find the minimum travel time for each pair.
Input
The first line gives the number of cities and the time a teleport takes .
The next lines give three integers describing the cities, in order from city 1 to city . If is 1, the city is special; if is 0, it is not special. is the coordinate of the city.
The next line gives , and the following lines each give two cities and .
Output
Print the minimum travel time from to , one value per line, over lines.
Constraints
- No two cities share the same coordinate.