Codeforces 214E Relay Race【Dp】
来源:互联网 发布:淘宝宝贝描述尺寸 编辑:程序博客网 时间:2024/06/05 16:07
Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
On a single line print a single number — the answer to the problem.
15
5
211 1416 12
53
325 16 2512 18 1911 13 8
136
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
题目大意:
从左上角只能向右向下走,有两个人一起走,求走过的格子的价值和,一个格子走过多次只能积累一次和。
问最大能够积累多大的和。
思路:
因为存在负权值,所以这个问题我们不能考虑用费用流去做。
我们考虑Dp。最简单最暴力的方法就是去设定Dp【i】【j】【x】【y】表示第一个人走到了(i,j)这个点,另一个人走到了(x,y)这个点能够获得的最优价值和。
但是考虑n比较大,O(n^4)去做不仅空间炸了,时间也炸了。
那么考虑设定Dp【i】【x】【step】表示第一个人走到了第i行,第二个人走到了第x行,一共走了step步的最优价值和,那么有:
当前step步,第一个人位子为:i,2+step-i,第二个人的位子为:x,2+step-x;
那么对于两个人,一共四种走法,转移一下有:
过程维护一下即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[350][350];int dp[305][305][305*2+5];int main(){ int n; while(~scanf("%d",&n)) { for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { for(int k=0;k<=2*n;k++) { dp[i][j][k]=-0x3f3f3f3f; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } dp[1][1][0]=a[1][1]; for(int i=1;i<=n;i++) { for(int x=1;x<=n;x++) { for(int step=1;step<=2*n-1;step++) { int j=2+step-i; int y=2+step-x; if(i==1&&j==1)continue; if(x==1&&y==1)continue; if(j>=1&&j<=n&&y>=1&&y<=n) { dp[i][x][step]=max(dp[i][x][step],dp[i-1][x-1][step-1]+a[i][j]+a[x][y]); dp[i][x][step]=max(dp[i][x][step],dp[i-1][x][step-1]+a[i][j]+a[x][y]); dp[i][x][step]=max(dp[i][x][step],dp[i][x-1][step-1]+a[i][j]+a[x][y]); dp[i][x][step]=max(dp[i][x][step],dp[i][x][step-1]+a[i][j]+a[x][y]); if(i==x&&j==y)dp[i][x][step]-=a[x][y]; } } } } printf("%d\n",dp[n][n][2*n-2]); }}
- Codeforces 214E. Relay Race dp
- Codeforces 214E Relay Race【Dp】
- codeforces 214E-Relay Race
- CodeForces 214E - Relay Race(二取方格数)
- codeforces 213C Relay Race(dp)
- CodeForces 213C Relay Race(dp)
- Codeforces Round #131 (Div. 2) E. Relay Race
- codeforces 213C Relay Race(DP基础,棋盘路径)
- CodeForces 213C Relay Race
- CodeForces 213C-Relay Race
- Codeforces 213C Relay Race
- codeforces 43E Race
- CodeForces213C.Relay Race
- 【dp】codeforces 83E
- codeforces-731E-dp
- Codeforces 748E dp
- Codeforces 722E [DP]
- Codeforces 416E DP
- CAD快速看图怎么看
- mhn调试经验
- Python 资源大全
- 搭建Hadoop(standlone模式)-mac
- React中的条件渲染
- Codeforces 214E Relay Race【Dp】
- 深度学习必备手册(上)
- 迷宫求解
- MATLAB 常用命令(函数)《转载》
- wap端使用a标签跳转不刷新问题
- android 常用权限和依赖
- HDU 5172 GTY's gay friends 线段树(排列,区间不同的数)
- IOS监听通话状态(私有API)
- Hive Multi Insert 引起的GC overhead limit exceeded