欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1700A.Optimal Path
Optimal Path
You are given a table of size . We will consider the table rows numbered from top to bottom from to , and the columns numbered from left to right from to . We will denote a cell that is in the -th row and in the -th column as . In the cell there is written a number , that is .
A turtle initially stands in the cell and it wants to come to the cell . From the cell it can in one step go to one of the cells or , if it exists. A path is a sequence of cells in which for every two adjacent in the sequence cells the following satisfies: the turtle can reach from the first cell to the second cell in one step. A cost of a path is the sum of numbers that are written in the cells of the path.

For example, with and the table will look as shown above. The turtle can take the following path: $(1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3)$. The cost of such way is equal to . On the other hand, the paths $(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 1)$ and are incorrect, because in the first path the turtle can't make a step , and in the second path it can't make a step .
You are asked to tell the turtle a minimal possible cost of a path from the cell to the cell . Please note that the cells and are a part of the way.
Input
The first line contains a single integer () — the number of test cases. The description of test cases follows.
A single line of each test case contains two integers and () — the number of rows and columns of the table respectively.
Output
For each test case output a single integer — a minimal possible cost of a path from the cell to the cell .
Note
In the first test case the only possible path consists of a single cell .
The path with the minimal cost in the second test case is shown in the statement.
In the fourth and the fifth test cases there is only one path from to . Both paths visit every cell in the table.
Samples
7
1 1
2 3
3 2
7 1
1 10
5 5
10000 10000
1
12
13
28
55
85
500099995000
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |