#P2658. 汽车拉力比赛
汽车拉力比赛
Description
Bo'ai City is going to hold a car rally.
The course is uneven, so it is represented by an grid of elevations . The elevation of each cell is between and .
Some cells are designated as checkpoints. The organizers want to assign a difficulty coefficient to the entire course such that, for any two checkpoints, there exists a path between them where the elevation difference between any pair of adjacent cells on that path does not exceed . In other words, is the minimum value that ensures all checkpoints are mutually reachable. Each cell is adjacent to the four cells to its north, south, east, and west.
Input Format
The first line contains two integers and .
Lines through : each line contains integers giving the elevations.
Lines through : each line contains integers, each either or ; indicates that the cell is a checkpoint.
Output Format
Output a single integer, the difficulty coefficient .
3 5
20 21 18 99 5
19 22 20 16 26
18 17 40 60 80
1 0 0 0 1
0 0 0 0 0
0 0 0 0 1
21
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号