Returning Home (Codeforces 1422D)
Tags: Others, shortest paths

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.

Input: 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$$$).

Output: In the only line print the minimum time required to get home.

Note: 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)$$$.

Sample Inputs:
5 3
1 1 5 5
1 2
4 1
3 3

84 5
67 59 41 2
39 56
7 2
15 3
74 18
22 7

Sample Outputs:
5

42

Solution (Python):
import heapq

def main():
    (n, m) = map(int, input().split())
    (sx, sy, fx, fy) = map(int, input().split())
    sub = [0] * (m + 2)
    sub[0] = (sx, sy)
    dx = []
    dy = []
    for i in range(m):
        (x, y) = map(int, input().split())
        dx.append((x, i + 1))
        dy.append((y, i + 1))
        sub[i + 1] = (x, y)
    dx.append((sx, 0))
    dy.append((sy, 0))
    dx.sort()
    dy.sort()
    G = [[] for i in range(m + 2)]
    d = [2 * 10 ** 9] * (m + 2)
    for i in range(m):
        (px, pindex) = dx[i]
        (nx, nindex) = dx[i + 1]
        G[pindex].append((nx - px, nindex))
        G[nindex].append((nx - px, pindex))
    for i in range(m):
        (py, pindex) = dy[i]
        (ny, nindex) = dy[i + 1]
        G[pindex].append((ny - py, nindex))
        G[nindex].append((ny - py, pindex))
    for i in range(m + 1):
        (x, y) = sub[i]
        min_cost = abs(y - fy) + abs(x - fx)
        G[m + 1].append((min_cost, i))
        G[i].append((min_cost, m + 1))
    que = []
    heapq.heapify(que)
    d[0] = 0
    heapq.heappush(que, (0, 0))
    while que:
        (cost, v) = heapq.heappop(que)
        if d[v] < cost:
            continue
        for (cost, node) in G[v]:
            if d[node] > d[v] + cost:
                d[node] = d[v] + cost
                heapq.heappush(que, (d[node], node))
    ans = d[m + 1]
    print(ans)
    return
if __name__ == '__main__':
    main()

Solution (Python):
import heapq

def main():
    (n, m) = map(int, input().split())
    (sx, sy, fx, fy) = map(int, input().split())
    sub = [0] * (m + 2)
    sub[0] = (sx, sy)
    dx = []
    dy = []
    for i in range(m):
        (x, y) = map(int, input().split())
        dx.append((x, i + 1))
        dy.append((y, i + 1))
        sub[i + 1] = (x, y)
    dx.append((sx, 0))
    dy.append((sy, 0))
    dx.sort()
    dy.sort()
    G = [[] for i in range(m + 2)]
    d = [2 * 10 ** 9] * (m + 2)
    for i in range(m):
        (px, pindex) = dx[i]
        (nx, nindex) = dx[i + 1]
        G[pindex].append((nx - px, nindex))
        G[nindex].append((nx - px, pindex))
    for i in range(m):
        (py, pindex) = dy[i]
        (ny, nindex) = dy[i + 1]
        G[pindex].append((ny - py, nindex))
        G[nindex].append((ny - py, pindex))
    for i in range(m + 1):
        (x, y) = sub[i]
        min_cost = abs(y - fy) + abs(x - fx)
        G[m + 1].append((min_cost, i))
        G[i].append((min_cost, m + 1))
    que = []
    heapq.heapify(que)
    d[0] = 0
    heapq.heappush(que, (0, 0))
    while que:
        (cost, v) = heapq.heappop(que)
        if d[v] < cost:
            continue
        for (cost, node) in G[v]:
            if d[node] > d[v] + cost:
                d[node] = d[v] + cost
                heapq.heappush(que, (d[node], node))
    ans = d[m + 1]
    print(ans)
    return
if __name__ == '__main__':
    main()

Solution (Python):
import heapq

def main():
    (n, m) = map(int, input().split())
    (sx, sy, fx, fy) = map(int, input().split())
    sub = [0] * (m + 2)
    sub[0] = (sx, sy)
    dx = []
    dy = []
    for i in range(m):
        (x, y) = map(int, input().split())
        dx.append((x, i + 1))
        dy.append((y, i + 1))
        sub[i + 1] = (x, y)
    dx.append((sx, 0))
    dy.append((sy, 0))
    dx.sort()
    dy.sort()
    G = [[] for i in range(m + 2)]
    d = [2 * 10 ** 9] * (m + 2)
    for i in range(m):
        (px, pindex) = dx[i]
        (nx, nindex) = dx[i + 1]
        G[pindex].append((nx - px, nindex))
        G[nindex].append((nx - px, pindex))
    for i in range(m):
        (py, pindex) = dy[i]
        (ny, nindex) = dy[i + 1]
        G[pindex].append((ny - py, nindex))
        G[nindex].append((ny - py, pindex))
    for i in range(m + 1):
        (x, y) = sub[i]
        min_cost = abs(y - fy) + abs(x - fx)
        G[m + 1].append((min_cost, i))
        G[i].append((min_cost, m + 1))
    que = []
    heapq.heapify(que)
    d[0] = 0
    heapq.heappush(que, (0, 0))
    while que:
        (cost, v) = heapq.heappop(que)
        if d[v] < cost:
            continue
        for (cost, node) in G[v]:
            if d[node] > d[v] + cost:
                d[node] = d[v] + cost
                heapq.heappush(que, (d[node], node))
    ans = d[m + 1]
    print(ans)
    return
if __name__ == '__main__':
    main()