A13832 | Returning Home
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 \times n$ square blocks. Yura needs to move from the block with coordinates $(s_x,s_y)$ to the block with coordinates $(f_x,f_y)$ . 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.
Let's represent the city as an area of $n \times n$ square blocks. Yura needs to move from the block with coordinates $(s_x,s_y)$ to the block with coordinates $(f_x,f_y)$ . 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.
输入格式
The first line contains two integers $n$ and $m$ — the size of the city and the number of instant-movement locations ( $1 \le n \le 10^9$ , $0 \le m \le 10^5$ ).
The next line contains four integers $s_x$ $s_y$ $f_x$ $f_y$ — 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 $x_i$ $y_i$ — coordinates of the $i$ -th instant-movement location ( $1 \le x_i, y_i \le n$ ).
The next line contains four integers $s_x$ $s_y$ $f_x$ $f_y$ — 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 $x_i$ $y_i$ — coordinates of the $i$ -th instant-movement location ( $1 \le x_i, y_i \le 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) \to (4, 2) \to (4, 3) \to (5, 3) \to (5, 4) \to (5, 5)$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted