[TJOI2015] Combinatorial Mathematics

Topic description

In order to improve his IQ, ZJY began to study combinatorial mathematics. One day she solved such a problem: give a grid diagram, some of which have treasures. Every time you start from the upper left corner, you can only go right or down. It takes at least a few walks to pick up all the treasures.

But she was not satisfied, and thought of a variant of this problem: suppose there are many pieces of treasure in each grid, and each time you pass a grid, you can only pick up one treasure. Pick up all the treasures?

She won't do it this time, can you help her?

Input and output format

Input format:

 

The first line is a positive integer t, indicating the number of data groups

The first row of each set of data is two positive integers n and m, indicating that the grid graph has n rows and m columns.

The next n lines, each with m non-negative integers, represent the number of treasures in this grid (0 means no treasure).

 

Output format:

 

For each set of data, output an integer representing at least the number of walks.

 

Input and output example

Input Example #1:  Copy
1
3 3
0 1 5
5 0 0
1 0 0
Output Sample #1:  Copy
10

illustrate

data range

For 30% of the data, n≤5.m≤5, the number of treasures in each grid does not exceed 5.

For 50% of the data, n≤100, m≤100, the number of treasures in each grid does not exceed 1000

For 100% data, n≤1000, m≤1000, the treasure in each grid does not exceed 10^6 pieces

Dilworth's Theorem, Minimum Chain Coverage = Maximum Backlink Length

So convert it into a reverse chain, use DP to find the largest reverse chain

The reverse chain is that the upper right point is connected to the lower left

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 typedef long long lol;
 8 lol f[1005][1005],a[1005][1005];
 9 int n,m;
10 int main()
11 {int i,j,T;
12 cin>>T;
13     while (T--)
14     {
15         memset(f,0,sizeof(f));
16         cin>>n>>m;
17         for (i=1;i<=n;i++)
18           for (j=1;j<=m;j++)
19             scanf("%lld",&a[i][j]);
20         for (i=1;i<=n;i++)
21         {
22             for (j=m;j>=1;j--)
23             f[i][j]=max(f[i-1][j+1]+a[i][j],max(f[i-1][j],f[i][j+1]));
24         }
25         cout<<f[n][1]<<endl;
26     }
27 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324940762&siteId=291194637