CF1713A.Traveling Salesman Problem

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).

More formally, if you are standing at the point (x,y)(x, y) , you can:

  • go left, and move to (x1,y)(x - 1, y) , or
  • go right, and move to (x+1,y)(x + 1, y) , or
  • go up, and move to (x,y+1)(x, y + 1) , or
  • go down, and move to (x,y1)(x, y - 1) .

There are nn boxes on this plane. The ii -th box has coordinates (xi,yi)(x_i,y_i) . It is guaranteed that the boxes are either on the xx -axis or the yy -axis. That is, either xi=0x_i=0 or yi=0y_i=0 .

You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point (0,0)(0,0) .

输入格式

The first line contains a single integer tt ( 1t1001 \le t \le 100 ) — the number of test cases.

The first line of each test case contains a single integer nn ( 1n1001 \le n \le 100 ) — the number of boxes.

The ii -th line of the following nn lines contains two integers xix_i and yiy_i ( 100xi,yi100-100 \le x_i, y_i \le 100 ) — the coordinate of the ii -th box. It is guaranteed that either xi=0x_i=0 or yi=0y_i=0 .

Do note that the sum of nn over all test cases is not bounded.

输出格式

For each test case output a single integer — the minimum number of moves required.

输入输出样例

  • 输入#1

    3
    4
    0 -2
    1 0
    -1 0
    0 2
    3
    0 2
    -3 0
    0 -1
    1
    0 0

    输出#1

    12
    12
    0

说明/提示

In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below.

$$$$(0,0) \to (1,0) \to (1,1) \to (1, 2) \to (0,2) \to (-1,2) \to (-1,1) \to (-1,0) \to (-1,-1) \to (-1,-2) \to (0,-2) \to (0,-1) \to (0,0) $$

In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below.

$$ (0,0) \to (0,1) \to (0,2) \to (-1, 2) \to (-2,2) \to (-3,2) \to (-3,1) \to (-3,0) \to (-3,-1) \to (-2,-1) \to (-1,-1) \to (0,-1) \to (0,0) $$$$In the third test case, we can collect all boxes without making any moves.

首页