loj#P3159. 「NOI2019」弹跳
「NOI2019」弹跳
Description
There are cities in the Kingdom of Flea, numbered from to , and city is the capital. All of the cities are located in a grid graph of size . Each city has an integer coordinate , and the coordinates of the cities are different from each other.
There are bouncing devices in the Kingdom of Flea, numbered from to . The -th device is located in city and with this device, a flea in city can jump to any city that satisfies , in time units.
Since that the cities are quite far away from each other, so fleas always travel by the bouncing devices. Specifically, during a trip a flea will pass through several cities in turn, whose indices are , respectively; in this trip, the indices of bouncing devices used in turn are . Each cities can appear any number of times in the sequence , and each bouncing devices can appear any number of times in the sequence and for each , bouncing devices is located in the city and the flea can jump to city with the help of the bouncer. We call this a trip from city to city , which costs time units.
Now the Flea King wants to know that for every city except the capital of the kingdom (city ), how much time it will at least take to get to the city from the capital.
Input
The first line contains integers .
Each of the next lines contains integers .
Each of the next lines contains integers , describing the information of the -th bouncing device.
It's guaranteed that every city is reachable from the capital.
Output
You should print lines, the -th line contains the minimum time it needs to get to city from the capital.
5 3 5 5
1 1
3 1
4 1
2 2
3 3
1 123 1 5 1 5
1 50 1 5 1 1
3 10 2 2 2 2
50
50
60
123
Limits And Hints
Partial scores are listed below.
测试点编号 | Additional Constraints | ||
---|---|---|---|
No | |||
Every bouncing devices can reach exactly one city, and |
|||
No | |||
No |
Please note that the memory limit of this problem is 128 MB.