2D Traveling (Codeforces 1869B)
Tags: Math, geometry

Piggy lives on an infinite plane with the Cartesian coordinate system on it.There are $$$n$$$ cities on the plane, numbered from $$$1$$$ to $$$n$$$, and the first $$$k$$$ cities are defined as major cities. The coordinates of the $$$i$$$-th city are $$$(x_i,y_i)$$$.Piggy, as a well-experienced traveller, wants to have a relaxing trip after Zhongkao examination. Currently, he is in city $$$a$$$, and he wants to travel to city $$$b$$$ by air. You can fly between any two cities, and you can visit several cities in any order while travelling, but the final destination must be city $$$b$$$.Because of active trade between major cities, it's possible to travel by plane between them for free. Formally, the price of an air ticket $$$f(i,j)$$$ between two cities $$$i$$$ and $$$j$$$ is defined as follows:$$$$$$ f(i,j)= \begin{cases} 0, & \text{if cities }i \text{ and }j \text{ are both major cities} \\ |x_i-x_j|+|y_i-y_j|, & \text{otherwise} \end{cases} $$$$$$Piggy doesn't want to save time, but he wants to save money. So you need to tell him the minimum value of the total cost of all air tickets if he can take any number of flights.

Input: The first line of input contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of test cases follows.The first line of each test case contains four integers $$$n$$$, $$$k$$$, $$$a$$$ and $$$b$$$ ($$$2\le n\le 2\cdot 10^5$$$, $$$0\le k\le n$$$, $$$1\le a,b\le n$$$, $$$a\ne b$$$) — the number of cities, the number of major cities and the numbers of the starting and the ending cities.Then $$$n$$$ lines follow, the $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9\le x_i,y_i\le 10^9$$$) — the coordinates of the $$$i$$$-th city. The first $$$k$$$ lines describe major cities. It is guaranteed that all coordinates are pairwise distinct.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

Output: For each test case, print a single integer — the minimum value of the total price of all air tickets.

Note: In the first test case:  The major cities are marked red. The optimal way to choose the flights is: $$$3\rightarrow 1 \rightarrow 2 \rightarrow 5$$$, which will cost $$$3+0+1=4$$$. Note that the flight $$$1\rightarrow 2$$$ costs $$$0$$$, because both city $$$1$$$ and $$$2$$$ are major cities.In the second test case, since there are only $$$2$$$ cities, the only way is to take a flight from city $$$1$$$ to $$$2$$$.In the third test case, since city $$$2$$$ and $$$4$$$ are both major cities, Piggy can directly take a flight from city $$$2$$$ to $$$4$$$, which costs $$$0$$$.In the fourth test case, Piggy can choose to take the following flights: $$$3\rightarrow 2\rightarrow 1$$$, and the cost is $$$11+11=22$$$.

Sample Inputs:
5
6 2 3 5
0 0
1 -2
-2 1
-1 3
2 -2
-3 -3
2 0 1 2
-1000000000 -1000000000
1000000000 1000000000
7 5 4 2
154 147
-154 -147
123 456
20 23
43 20
998 244
353 100
3 1 3 1
0 10
1 20
2 30
4 3 2 4
0 0
-100 100
-1 -1
-1 0

Sample Outputs:
4
4000000000
0
22
1

Solution (Python):
from io import StringIO
import sys

def nc(x, y, a):
    c = 10 ** 11
    for (l, m) in a:
        d = abs(l - x) + abs(m - y)
        if c is None or d < c:
            c = d
    return c

def main():
    out = StringIO()
    get_line = sys.stdin.readline

    def get_ints():
        return map(int, get_line().split(' '))

    def get_int():
        return int(get_line())
    for i in range(get_int()):
        (n, k, a, b) = get_ints()
        c = []
        for j in range(n):
            (x, y) = get_ints()
            c.append((x, y))
        mc = c[:k]
        dd = abs(c[a - 1][0] - c[b - 1][0]) + abs(c[a - 1][1] - c[b - 1][1])
        fad = nc(*c[a - 1], mc)
        fbd = nc(*c[b - 1], mc)
        ans = min(dd, fad + fbd)
        out.write(str(ans))
        out.write('\n')
    sys.stdout.write(out.getvalue())
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
print = sys.stdout.write

def dist(a, b):
    return abs(a[0] - b[0]) + abs(a[1] - b[1])

def solve():
    (n, k, src, dst) = map(int, input().split())
    src -= 1
    dst -= 1
    p = [tuple(map(int, input().split())) for _ in range(n)]
    minsrc = float('inf')
    mindst = float('inf')
    for i in range(k):
        minsrc = min(minsrc, dist(p[src], p[i]))
        mindst = min(mindst, dist(p[dst], p[i]))
    fly = dist(p[src], p[dst])
    nofly = minsrc + mindst
    return min(fly, nofly)
print('\n'.join(map(str, (solve() for _ in range(int(input()))))))

Solution (Python):
import sys

def solve():
    (n, k, s, t) = map(int, input().split())
    x = [0] * (n + 1)
    y = [0] * (n + 1)
    for i in range(1, n + 1):
        (x[i], y[i]) = map(int, input().split())
    ans = abs(x[s] - x[t]) + abs(y[s] - y[t])
    min_s = float('inf')
    min_t = float('inf')
    for i in range(1, k + 1):
        min_s = min(min_s, abs(x[s] - x[i]) + abs(y[s] - y[i]))
        min_t = min(min_t, abs(x[t] - x[i]) + abs(y[t] - y[i]))
    ans = min(ans, min_s + min_t)
    print(ans)

def main():
    T = int(input())
    for _ in range(T):
        solve()
if __name__ == '__main__':
    main()