light oj 1036 - A Refining Company(DP求最大价值和)


1036 - A Refining Company
Time Limit: 3 second(s)Memory Limit: 32 MB

Its year 2200, planet Earth is out of resources and people are relying on the resources from other planets. There are several Refining Companies who collect these resources from other planets and bring them back to Earth. The task may sound simple, but in reality it's a challenging job. The resources are scattered and after collecting them, they have to be taken to a place where they can be refined. Since some minerals are extremely dangerous, the whole process should be done very carefully. A single tiny mistake can cause a massive explosion resulting in a huge loss.

You work in such a company who collects Uranium and Radium from planet Krypton. These minerals are used for generating powers. For simplicity you have divided planet Krypton into cells that form a matrix of m rows and n columns, where the rows go from east to west and the columns go from north to south. Your advanced mine detector has detected the approximate amount of Radium and Uranium in each cell. Your company has built two refining factories, one in West and the other in North. The factory in North is used to refine Radium and the factory in West is used to refine Uranium. Your task is to design the conveyor belt system that will allow them to mine the largest amount of minerals.

There are two types of conveyor belts: the first moves minerals from east to west, the second moves minerals from south to north. In each cell you can build either type of conveyor belt, but you cannot build both of them in the same cell. If two conveyor belts of the same type are next to each other, then they can be connected. For example, the Radium mined at a cell can be transported to the Radium refinement factory via a series of south-north conveyor belts.

The minerals are very unstable, thus they have to be brought to the factories on a straight path without any turns. This means that if there is a south-north conveyor belt in a cell, but the cell north of it contains an east-west conveyor belt, then any mineral transported on the south-north conveyor belt will be lost. The minerals mined in a particular cell have to be put on a conveyor belt immediately; in the same cell (thus they cannot start the transportation in an adjacent cell). Furthermore, any Radium transported to the Uranium refinement factory will be lost, and vice versa.

Your program has to design a conveyor belt system that maximizes the total amount of minerals mined, i.e., the sum of the amount of Radium transported to the Radium refinery and the amount of Uranium to the Uranium refinery.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case begins with a blank line and two integers: m - the number of rows, and n - the number columns (1 ≤ m, n ≤ 500). The next m lines describe the amount of Uranium that can be found in the cells. Each of these m lines contains n integers. The first line corresponds to the northernmost row; the first integer of each line corresponds to the westernmost cell of the row. The integers are between 0 and 1000. The next mlines describe in a similar fashion the amount of Radium found in the cells. Data set is huge, so use faster i/o methods.

Output

For each case of input you have to print the case number and the maximum amount of minerals you can collect.

Sample Input

Output for Sample Input

2

 

4 4

0 0 10 9

1 3 10 0

4 2 1 3

1 1 20 0

10 0 0 0

1 1 1 30

0 0 5 5

5 10 10 10

 

2 3

5 10 34

0 0 0

0 0 0

50 0 0

Case 1: 98

Case 2: 50

Note

Dataset is huge. Use faster I/O methods.


题目大意:

一个m*n的矩阵,里面有两种矿物质铀和镭,现在要把铀和镭运送到指定位置。北边是炼镭厂,西边是了炼铀厂。
现在要建立传送带,传送带有两种,一种是从东到西,另一种是从南到北,传送带不能交叉,并且运送中途不能中断。现在你要计算出最多能采集多少矿。
输入数据:第一个m*n的矩阵代表铀的矿物质分布, 第二个矩阵代表镭的矿物质分布。
这题一开始把题目看错了,虽然是两个矩阵,但是一个地图,用一个二维DP记录下标的控制范围内的最优值,然后用不被影响范围内的值去更新新的状态。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 510;
int  dp[N][N], v1[N][N], v2[N][N];


int main()
{
    int t, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        int m, n;
        scanf("%d %d", &m, &n);
        memset(v1,0,sizeof(v1));
        memset(v2,0,sizeof(v2));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d", &v1[i][j]);
                v1[i][j]+=v1[i][j-1];
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d", &v2[i][j]);
                v2[i][j]+=v2[i-1][j];
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=max(dp[i-1][j]+v1[i][j],dp[i][j-1]+v2[i][j]);
            }
        }
        printf("Case %d: %d\n", ncase++, dp[m][n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值