Time Limit:3000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
Description
![Download as PDF Download as PDF](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM Finals this year). This is the place where the astronauts are trained for Mission Seven Dwarfs, the next giant leap in space exploration. The Mars Odyssey program revealed that the surface of Mars is very rich in yeyenum and bloggium. These minerals are important ingredients for certain revolutionary new medicines, but they are extremely rare on Earth. The aim of Mission Seven Dwarfs is to mine these minerals on Mars and bring them back to Earth.
The Mars Odyssey orbiter identified a rectangular area on the surface of Mars that is rich in minerals. The area is divided into cells that form a matrix of n rows and m columns, where the rows go from east to west and the columns go from north to south. The orbiter determined the amount of yeyenum and bloggium in each cell. The astronauts will build a yeyenum refinement factory west of the rectangular area and a bloggium factory to the north. 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 bloggium mined at a cell can be transported to the bloggium 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 beltwill 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 bloggium transported to the yeyenum refinement factory will be lost, and vice versa.
![\epsfbox{p3530.eps}](https://icpcarchive.ecs.baylor.edu/external/35/p3530.gif)
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 yeyenum transported to the yeyenum refinery and the amount of bloggium transported to the bloggium refinery.
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1n
500 of rows, and the number 1
m
500 of columns. The next n lines describe the amount of yeyenum that can be found in the cells. Each of these n lines contains m 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 n lines describe in a similar fashion theamount of bloggium found in the cells.
The input is terminated by a block with n = m = 0.
Output
For each test case, you have to output a single integer on a separate line: the maximum amount of mineralsthat can be mined.
Sample Input
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 0 0
Sample Output
98
题意 :
n*m的网格中有A矿和B矿的数量,A矿必须右到左运输,B矿必须下到上运输,管子不能拐弯或者中断,必须一直到最左/最上,要求收集到的AB矿石总量最大。
思路:
简单DP
DP[i][j][k]表示格子[1, 1]到][i,j]的格子并且格子[i, j]向左k=0或者向上k=1能收集到的最大值
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int maxn = 500 + 20;
int sumV[maxn][maxn];
int sumH[maxn][maxn];
int dp[maxn][maxn][2];
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF && (n || m)) {
sumV[0][0] = sumH[0][0] = 0;
FOR(i, 1, n) FOR(j, 1, m) {
int v;
scanf("%d", &v);
sumH[i][j] = sumH[i][j-1] + v;
}
FOR(i, 1, n) FOR(j, 1, m) {
int v;
scanf("%d", &v);
sumV[i][j] = sumV[i-1][j] + v;
}
REP(i, n+1) dp[i][0][0] = dp[i][0][1] = 0;
REP(i, m+1) dp[0][i][0] = dp[0][i][1] = 0;
FOR(i, 1, n) FOR(j, 1, m) {
dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1]) + sumH[i][j];
dp[i][j][1] = max(dp[i][j-1][0], dp[i][j-1][1]) + sumV[i][j];
}
int ans = max(dp[n][m][0], dp[n][m][1]);
printf("%d\n", ans);
}
return 0;
}