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×nn×n square blocks. Yura needs to move from the block with coordinates (sx,sy)(sx,sy) to the block with coordinates (fx,fy)(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 mm 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 xx or with the same coordinate yy as the location.
Help Yura to find the smallest time needed to get home.
Input
The first line contains two integers nn and mm — the size of the city and the number of instant-movement locations (1≤n≤1091≤n≤109, 0≤m≤1050≤m≤105).
The next line contains four integers sxsx sysy fxfx fyfy — the coordinates of Yura's initial position and the coordinates of his home (1≤sx,sy,fx,fy≤n1≤sx,sy,fx,fy≤n).
Each of the next mm lines contains two integers xixi yiyi — coordinates of the ii-th instant-movement location (1≤xi,yi≤n1≤xi,yi≤n).
Output
In the only line print the minimum time required to get home.
Examples
Input
5 3
1 1 5 5
1 2
4 1
3 3
Output
5
Input
84 5
67 59 41 2
39 56
7 2
15 3
74 18
22 7
Output
42
Note
In the first example Yura needs to reach (5,5)(5,5) from (1,1)(1,1). He can do that in 55 minutes by first using the second instant-movement location (because its yy coordinate is equal to Yura's yy coordinate), and then walking (4,1)→(4,2)→(4,3)→(5,3)→(5,4)→(5,5)(4,1)→(4,2)→(4,3)→(5,3)→(5,4)→(5,5).
题意:
有一个n * n的网格,一个人要从(sx, sy)去往(fx, fy),网格中有一些点是特殊点,与特殊点在同一行或同一列上的点,可以直接到达特殊点,花费为0,从任何一个点都可以上下左右移动,每移动一格花费1,问最少花费多少。
思路:
把特殊点所在的行和列当作点
(1)特殊点向它们所在的行和列连双向边,花费都为0(特殊点到列为0因为本来就在该行该列,列到特殊点为0因为可以直接到特殊点);
(2)起点向它所在的行列连边
(3)出现的行之间连双向边,花费为两行之间的距离。假如出现了x个行数,共连x - 1条边(两两连边没有必要并且会炸掉你的边集数组导致RE)
遍历一遍特殊点,答案就是min(起点到特殊点的最短路 + 从特殊点走到终点)
(当然还可能从起点直接走到终点,别忘了取min
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 3e5 + 7;
const ll M = 8e5 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll head[N];
bool vis[N];
ll dis[N];
ll n, m, tot;
struct node {
ll x, y;
}s[N];
struct Node {
ll u, v, l, next;
}edge[M];
struct A{
ll pos, cost;
bool operator < (const A &a)const {
return cost > a.cost;
}
};
void init() {
tot = 1;
memset(head, -1, sizeof(head));
}
void addedge(ll x, ll y, ll z) {
edge[tot].u = x;
edge[tot].v = y;
edge[tot].l = z;
edge[tot].next = head[x];
head[x] = tot++;
}
void dijk(ll src) {
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
priority_queue<A>q;
dis[src] = 0;
A now;
now.cost = 0;
now.pos = src;
q.push(now);
while(!q.empty()) {
now = q.top();
q.pop();
if(vis[now.pos])
continue;
vis[now.pos] = 1;
for(ll i = head[now.pos]; ~i; i = edge[i].next) {
ll to = edge[i].v;
if(!vis[to] && dis[to] > edge[i].l + dis[edge[i].u]) {
dis[to] = dis[edge[i].u] + edge[i].l;
now.cost = dis[to];
now.pos = to;
q.push(now);
}
}
}
}
ll stx[N], sty[N], totx, toty;
int main() {
init();
totx = 0, toty = 0;
scanf("%lld%lld", &n, &m);
for(ll i = 1; i <= m + 2; ++i) {
scanf("%lld%lld", &s[i].x, &s[i].y);
stx[++totx] = s[i].x;
sty[++toty] = s[i].y;
}
sort(stx + 1, stx + totx + 1);
totx = unique(stx + 1, stx + totx + 1) - stx - 1;
sort(sty + 1, sty + toty + 1);
toty = unique(sty + 1, sty + toty + 1) - sty - 1;
n = m + 2;
map<ll, ll>mx, my;
for(ll i = 1; i <= totx; ++i) {
mx[stx[i]] = ++n;
if(i > 1) {
ll x = stx[i], y = stx[i - 1];
addedge(mx[x], mx[y], abs(x - y));
addedge(mx[y], mx[x], abs(x - y));
}
}
for(ll i = 1; i <= toty; ++i) {
my[sty[i]] = ++n;
ll x = sty[i], y = sty[i - 1];
addedge(my[x], my[y], abs(x - y));
addedge(my[y], my[x], abs(x - y));
}
addedge(1, mx[s[1].x], 0);
addedge(1, my[s[1].y], 0);
for(ll i = 3; i <= m + 2; ++i) {
addedge(i, mx[s[i].x], 0);
addedge(mx[s[i].x], i, 0);
addedge(i, my[s[i].y], 0);
addedge(my[s[i].y], i, 0);
}
dijk(1);
ll minn = inf;
for(ll i = 3; i <= m + 2; ++i) {
minn = min(minn, dis[i] + abs(s[2].x - s[i].x) + abs(s[2].y - s[i].y));
}
minn = min(minn, abs(s[2].x - s[1].x) + abs(s[2].y - s[1].y));
printf("%lld\n", minn);
mx.clear(), my.clear();
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/4393165/blog/4664414