poj 2948 -- Martian Mining(dp)
来源:互联网 发布:网络群众路线怎么做 编辑:程序博客网 时间:2024/05/18 01:50
Martian Mining
Time Limit: 5000MSMemory Limit: 65536KTotal Submissions: 2053Accepted: 1236
Description
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.
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.
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.
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 1 ≤ n ≤ 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.
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 40 0 10 91 3 10 04 2 1 3 1 1 20 010 0 0 01 1 1 300 0 5 55 10 10 100 0
Sample Output
98
Hint
Huge input file, 'scanf' recommended to avoid TLE.
Source
Central Europe 2005
这道题的关键是要找出递推公式
对于每个包含最右下角的矩阵,
设其左上角为(i,j),右下角为(1,1)
若(i,j)选择向上,则这个第i行,从j开始的后面所有管道一定都是向上的,因为向左不能产生任何效益
若(i,j)选择向左,则这个第j列,从i开始的下面所有管道一定都是向左的,因为向上不能产生任何效益
这样就很容易看出来转移方程了:
dp[i][j] = max(sumb[i][j]+dp[i-1][j],suma[i][j]+dp[i][j-1]);
详见代码
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int A[505][505];int B[505][505];int suma[505][505];int sumb[505][505];int dp[505][505];int main(){ int n,m; while(1){ scanf("%d%d",&n,&m); if(n == 0 && m == 0) break; //输入,最左上角下标是(n,m),右下角(1,1) for(int i = n;i > 0;i--){ for(int j = m;j > 0;j--){ scanf("%d",&A[i][j]); } } for(int i = n;i > 0;i--){ for(int j = m;j > 0;j--){ scanf("%d",&B[i][j]); } } //预处理出第i行,j及其后面所有B元素和sumb //和第j列,i及其下面所有A元素和suma for(int i = 1;i <= n;i++) suma[1][i] = A[1][i]; for(int j = 1;j <= m;j++) sumb[j][1] = B[j][1]; for(int i = 2;i <= n;i++){ for(int j = 1;j <= m;j++){ suma[i][j] = suma[i-1][j] + A[i][j]; } } for(int i = 1;i <= n;i++){ for(int j = 2;j <= m;j++){ sumb[i][j] = sumb[i][j-1] + B[i][j]; } } //进行dp递推 //先初始化边界 for(int i = 1;i <= n;i++) dp[i][0] = 0; for(int j = 1;j <= m;j++) dp[0][j] = 0; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ dp[i][j] = max(sumb[i][j]+dp[i-1][j],suma[i][j]+dp[i][j-1]); } } printf("%d\n",dp[n][m]); } return 0;}
- poj 2948 -- Martian Mining(dp)
- POJ 2948 Martian Mining DP
- POJ 2948 Martian Mining(DP)
- [poj 2948] Martian Mining 棋盘型DP
- Poj 2948 Martian Mining
- POJ-2948-Martian Mining
- POJ 2948 Martian Mining
- POJ 2948 Martian Mining
- poj 2948 Martian Mining
- poj 2948 Martian Mining
- POJ 2948 Martian Mining
- poj 2984Martian Mining(dp)
- poj 2948 Martian Mining dp
- POJ 2948 Martian Mining(简单的二维dp)
- UVA 1366 - Martian Mining(dp)
- UVaLive 3530 - Martian Mining (简单DP)
- UVA1366-----Martian Mining------DP
- poj2948 Martian Mining dp
- 数学模型的类型
- 广而告之-双重检查锁定模式
- setsockopt函数
- Three ways of Computing Surface Normals
- android和iOS平台的崩溃捕获和收集
- poj 2948 -- Martian Mining(dp)
- LD_PRELOAD的使用--调试代码
- 输入基数b和十进制正整数n,转换进制
- 系统服务详解之时间服务1
- (转)Fine-Grained Access to Network Services in Oracle Database 11g Release 1
- 汽水瓶
- HTML标签详解
- Java程序操作Oracle两种方式之简单实现
- oracle中连接mysql