CF1869B.2D Traveling

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Piggy lives on an infinite plane with the Cartesian coordinate system on it.

There are nn cities on the plane, numbered from 11 to nn, and the first kk cities are defined as major cities. The coordinates of the ii-th city are (xi,yi)(x_i,y_i).

Piggy, as a well-experienced traveller, wants to have a relaxing trip after Zhongkao examination. Currently, he is in city aa, and he wants to travel to city bb 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 bb.

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)f(i,j) between two cities ii and jj 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.

小猪生活在一个带有笛卡尔坐标系的无限平面上。

平面上有 nn 座城市,编号从 11 到 nn,其中前 kk 座城市被定义为“主要城市”。第 ii 座城市的坐标为 (xi,yi)(x_i, y_i)。

作为一位经验丰富的旅行者,小猪希望在中考结束后来一场轻松的旅行。目前他位于城市 aa,并希望乘飞机前往城市 bb。你可以在任意两座城市之间直飞,旅行途中可以按任意顺序访问若干中间城市,但最终目的地必须是城市 bb。

由于主要城市之间贸易活跃,它们之间的航班可免费乘坐。形式化地,城市 ii 与城市 jj 之间的机票价格 f(i,j)f(i,j) 定义如下:

f(i,j)={0,若城市 i 和 j 均为主要城市∣xi−xj∣+∣yi−yj∣,否则f(i,j)= \begin{cases} 0, & \text{若城市 }i \text{ 和 }j \text{ 均为主要城市} \\ |x_i-x_j|+|y_i-y_j|, & \text{否则} \end{cases}

小猪并不追求节省时间,而只希望节省金钱。因此你需要告诉他:在允许任意次数飞行的前提下,所有机票费用之和的最小值是多少?

输入格式

The first line of input contains a single integer tt (1≤t≤1041\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 nn, kk, aa and bb (2≤n≤2⋅1052\le n\le 2\cdot 10^5, 0≤k≤n0\le k\le n, 1≤a,b≤n1\le a,b\le n, a≠ba\ne b) — the number of cities, the number of major cities and the numbers of the starting and the ending cities.

Then nn lines follow, the ii-th line contains two integers xix_i and yiy_i (−109≤xi,yi≤109-10^9\le x_i,y_i\le 10^9) — the coordinates of the ii-th city. The first kk lines describe major cities. It is guaranteed that all coordinates are pairwise distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4)—— 表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含四个整数 nn、kk、aa 和 bb(2≤n≤2⋅1052\le n\le 2\cdot 10^5,0≤k≤n0\le k\le n,1≤a,b≤n1\le a,b\le n,a≠ba\ne b)—— 分别表示城市的数量、主要城市的数量,以及起点城市和终点城市的编号。

接下来是 nn 行,其中第 ii 行包含两个整数 xix_i 和 yiy_i(−109≤xi,yi≤109-10^9\le x_i,y_i\le 10^9)—— 表示第 ii 个城市的坐标。前 kk 行描述的是主要城市。保证所有坐标的两两互不相同。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot 10^5。

输出格式

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

对于每个测试用例,输出一个整数——所有机票总价的最小值。

输入输出样例

  • 输入#1

    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

    输出#1

    4
    4000000000
    0
    22
    1

说明/提示

In the first test case:

The major cities are marked red.

The optimal way to choose the flights is: 3→1→2→53\rightarrow 1 \rightarrow 2 \rightarrow 5, which will cost 3+0+1=43+0+1=4. Note that the flight 1→21\rightarrow 2 costs 00, because both city 11 and 22 are major cities.

In the second test case, since there are only 22 cities, the only way is to take a flight from city 11 to 22.

In the third test case, since city 22 and 44 are both major cities, Piggy can directly take a flight from city 22 to 44, which costs 00.

In the fourth test case, Piggy can choose to take the following flights: 3→2→13\rightarrow 2\rightarrow 1, and the cost is 11+11=2211+11=22.

在第一个测试用例中:

主要城市以红色标出。

选择航班的最优方案为:3→1→2→53\rightarrow 1 \rightarrow 2 \rightarrow 5,总花费为 3+0+1=43+0+1=4。注意,航班 1→21\rightarrow 2 的花费为 00,因为城市 11 和城市 22 均为主要城市。

在第二个测试用例中,由于仅有 22 座城市,唯一的方式是从城市 11 飞往城市 22。

在第三个测试用例中,由于城市 22 和城市 44 均为主要城市,Piggy 可直接从城市 22 飞往城市 44,花费为 00。

在第四个测试用例中,Piggy 可选择以下航班路线:3→2→13\rightarrow 2\rightarrow 1,总花费为 11+11=2211+11=22。

输入解题思路,AI测评打分。不知道怎么写?

首页