#P3974. [TJOI2015] 组合数学

    ID: 2903 远端评测题 2000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学递推2015各省省选线性递推,递推式天津

[TJOI2015] 组合数学

Description

To improve her IQ, ZJY started studying combinatorics. One day she solved this problem: given a grid where some cells contain treasure. Each time, starting from the top-left corner, you can only move right or down. What is the minimum number of walks needed so that it is possible to collect all the treasures?

But she was not satisfied and thought of a variation: suppose each cell contains many pieces of treasure, and on each pass through a cell you can take at most one piece of treasure. With other conditions unchanged, what is the minimum number of walks needed so that it is possible to collect all the pieces of treasure?

This time she could not solve it. Can you help her?

Input Format

The first line contains a positive integer tt, the number of test cases.

For each test case, the first line contains two positive integers nn and mm, indicating the grid has nn rows and mm columns.

The next nn lines each contain mm non-negative integers, representing the number of pieces of treasure in that cell (00 means no treasure).

Output Format

For each test case, output one integer, the minimum number of walks.

1
3 3
0 1 5
5 0 0
1 0 0
10

Hint

Constraints

For 30%30\% of the testdata, n5n \le 5, m5m \le 5, and the number of pieces of treasure in each cell does not exceed 55.

For 50%50\% of the testdata, n100n \le 100, m100m \le 100, and the number of pieces of treasure in each cell does not exceed 10001000.

For 100%100\% of the testdata, 1t21 \le t \le 2, 1n10001 \le n \le 1000, 1m10001 \le m \le 1000, and the number of pieces of treasure in each cell does not exceed 10610^6.

Translated by ChatGPT 5