#P9830. [ICPC2020 Shanghai R] Traveling in the Grid World

    ID: 9194 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>数学贪心2020上海Special JudgeO2优化ICPC

[ICPC2020 Shanghai R] Traveling in the Grid World

题目描述

Consider a grid pattern with nn rows and mm columns. There are (n+1)×(m+1)(n+1)\times(m+1) grid points in total which is the intersections of n+1n+1 horizontal lines and m+1m+1 vertical lines. We number the horizontal lines from 00 to nn from top to bottom. We number the vertical lines from 00 to mm from left to right. The intersection of horizontal line ii and vertical line jj is named (i,j)(i, j) (0in,0jm0\le i\le n, 0\le j\le m).

There are some constraints when you travel in the grid world. When you are located at point (x,y)(x,y), you can choose a destination (x,y)(x',y') and walk to it along the line segment between (x,y)(x, y) and (x,y)(x', y'). We call this operation a walk\textit{walk}. A walk is forbidden if there exists another grid point different from (x,y)(x, y) and (x,y)(x', y') lying on the line segment between them. You can walk as many times as you want but the directions of two consecutive walks cannot be the same. (Specifically, if you walk from (x0,y0)(x_0, y_0) to (x1,y1)(x_1, y_1) and then walk from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2), you must make sure that (x0x1)(y1y2)(x1x2)(y0y1)(x_0-x_1)(y_1-y_2)\neq (x_1-x_2)(y_0-y_1).) The length of a walk from (x,y)(x, y) to (x,y)(x', y') is defined as the Euclidean distance between the two endpoints, (xx)2+(yy)2\sqrt{(x-x')^2+(y'-y)^2}.

Starting from (0,0)(0,0), you are planning to arrive at (n,m)(n,m) by several walks. Because of the annoying rules, you may need some turning points to achieve your goal. Please find the minimum total length of your walks.

输入格式

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains two integers n,mn,m (1n,m1061\le n,m \le 10^6) indicating the size of the grid graph.

It is guaranteed that the sum of the values of max(n,m)\max(n,m) over all test cases does not exceed 10610^6.

输出格式

For each test case, output the minimum total length of walks. Your answer will be considered correct if its absolute or relative error does not exceed 10910 ^{-9}.

题目大意

题目描述

考虑具有 nn 行和 mm 列的网格模式。总共有n+1×m+1(n+1)\times(m+1)网格点,即n+1n+1水平线和m+1m+1垂直线的交点。我们将水平线从上到下从 00nn 编号。我们将垂直线从左到右编号从 00mm。水平线 ii 和垂直线 jj 的交点命名为 (i,j)(i,j)(0in0jm0 \le i \le n, 0 \le j \le m)。

当您在网格世界中旅行时,会有一些限制。当您位于点 (x,y)(x,y) 时,您可以选择一个目的地 (x,y)(x',y') 并沿着 (x,y)(x,y)(x,y)(x',y') 之间的线段步行到该目的地。我们将此操作称为 walk\textit{walk}。如果它们之间的线段上存在另一个不同于 (x,y)(x,y)(x,y)(x', y') 的网格点,则禁止行走。您可以随心所欲地走很多次,但连续两次步行的方向不能相同。(具体来说,如果从 (x0,y0)(x_0,y_0)(x1,y1)(x_1, y_1),然后从 (x1,y1)(x_1, y_1)x2y2(x_2, y_2),则必须确保 (x0x1)(y1y2)(x1x2)(y0y1)(x_0-x_1)(y_1-y_2) \neq (x_1-x_2)(y_0-y_1)。从 (x,y)(x,y)(x,y)(x',y') 的步行长度定义为两个端点之间的欧几里得距离 (xx)2+(yy)2\sqrt{(x-x')^2+(y'-y)^2}

(0,0)(0,0) 开始,您计划通过几次步行到达 (nm)(n,m)。由于烦人的规则,您可能需要一些转折点来实现您的目标。请找到您步行的最小总长度。

输入格式

有多个测试用例。输入的第一行包含一个整数 TT,表示测试用例的数量。对于每个测试用例, 第一行包含两个整数 n,mn,m1n,m1061 \le n,m \le 10^6),表示网格图的大小。

保证所有测试用例中 max(n,m)\max(n,m) 的值之和不超过 10610^6

2
2 2
2 3
3.236067977499790
3.605551275463989