CF1422D.Returning Home
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city.
Let's represent the city as an area of n×n square blocks. Yura needs to move from the block with coordinates (sx,sy) to the block with coordinates (fx,fy). In one minute Yura can move to any neighboring by side block; in other words, he can move in four directions. Also, there are m instant-movement locations in the city. Their coordinates are known to you and Yura. Yura can move to an instant-movement location in no time if he is located in a block with the same coordinate x or with the same coordinate y as the location.
Help Yura to find the smallest time needed to get home.
尤拉已经步行了一段时间,现在正计划回家。他需要尽快回到家。为此,尤拉可以利用城市中若干个瞬移点。
我们将城市建模为一个 n×n 的正方形街区网格。尤拉需要从坐标为 (sx,sy) 的街区出发,前往坐标为 (fx,fy) 的街区。每分钟,尤拉可移动至与其当前所在街区共用一条边的任意相邻街区;即,他每次只能向四个基本方向(上、下、左、右)之一移动一格。此外,城市中还有 m 个瞬移点,其坐标已知(对尤拉和你均可见)。若尤拉当前所处街区的横坐标 x 或纵坐标 y 与某个瞬移点的对应坐标相同,则他可瞬间(耗时为 0)移动到该瞬移点。
请帮助尤拉计算其回家所需的最短时间。
输入格式
The first line contains two integers n and m — the size of the city and the number of instant-movement locations (1≤n≤109, 0≤m≤105).
The next line contains four integers sx sy fx fy — the coordinates of Yura's initial position and the coordinates of his home ($ 1 \le s_x, s_y, f_x, f_y \le n$).
Each of the next m lines contains two integers xi yi — coordinates of the i-th instant-movement location (1≤xi,yi≤n).
第一行包含两个整数 n 和 m —— 城市的大小以及瞬移位置的数量(1≤n≤109,0≤m≤105)。
第二行包含四个整数 sx、sy、fx、fy —— 尤拉的初始位置坐标及其家的坐标(1≤sx,sy,fx,fy≤n)。
接下来的 m 行中,每行包含两个整数 xi、yi —— 第 i 个瞬移位置的坐标(1≤xi,yi≤n)。
输出格式
In the only line print the minimum time required to get home.
在唯一的一行中输出回家所需的最短时间。
输入输出样例
输入#1
5 3 1 1 5 5 1 2 4 1 3 3
输出#1
5
输入#2
84 5 67 59 41 2 39 56 7 2 15 3 74 18 22 7
输出#2
42
说明/提示
In the first example Yura needs to reach (5,5) from (1,1). He can do that in 5 minutes by first using the second instant-movement location (because its y coordinate is equal to Yura's y coordinate), and then walking (4,1)→(4,2)→(4,3)→(5,3)→(5,4)→(5,5).
在第一个例子中,尤拉需要从 (1,1) 到达 (5,5)。他可以通过以下方式在 5 分钟内完成:首先使用第二个瞬移点(因为其 y 坐标与尤拉当前的 y 坐标相等),然后步行 (4,1)→(4,2)→(4,3)→(5,3)→(5,4)→(5,5)。
输入解题思路,AI测评打分。不知道怎么写?