codeforces.com/problemset/problem/213/C
虽然一开始就觉得从右下角左上角直接dp2次是不行的,后面还是这么写了WA了
两次最大的并不一定是最大的,这个虽然一眼就能看出,第一次可能会影响第二次让第二次太小。
这是原因。
5
4 32 1 18 41
47 38 7 43 43
48 23 39 40 23
26 39 33 5 36
31 29 7 26 47
这组数据是结果。
走完第一遍成
0 32 1 18 41
0 38 7 43 43
0 0 0 0 0
26 39 33 5 0
31 29 7 26 0
这样倒着走回去一定会经过0导致第二遍小很多。
正确走法
5
0 32 1 18 41
0 38 7 43 43
0 23 39 40 23
0 0 0 5 36
31 29 0 0 0
然后
5
0 0 1 18 41
0 0 7 43 43
0 0 0 0 0
0 0 0 5 0
31 29 0 0 0
这样和为508比两次取最大的482还大。
完全想不到怎么弄了。上一次求两次和最短路的走过了后就不能走,也想成两次跑最小,也是跪,结果应该用最小费用流。

这个也是没发现,不管怎么走一定每次确定步数后都会在同一条对角线上。
所以可以一条对角线一条一条的推。然而要同时确定两条路,走过去再走回来就等于走过去两条路,经过同一个点的时候只取一次。
dp[d][i][j]表示到第d条对角线时第一条路在i行,第二条路在j行最大的和能取多大,一路推到最后,这里可以用滚动数组dp内存就只有n*n了。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 305;
const ll mod = 1e9 + 7;
const double eps = 1e-12;
int a[N][N];
int dp[2][N][N];
int main() {
int n;cin >> n;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
cin >> a[i][j];
memset(dp, -2, sizeof dp);
dp[1][1][1] = a[1][1];
int now = 1;
for (int d = 2;d < n + n;d++) {
now ^= 1;
for (int i = max(1,d-n+1);i <= min(n,d);i++)
for (int j = max(1,d-n+1);j <= min(n,d);j++) {
for (int x = i - 1;x <= i;x++) {
for (int y = j - 1;y <= j;y++) {
if (x >= max(1, d - n) && x <= min(n, d - 1) && y >= max(1,d - n) && y <= min(n, d - 1)) {
int val = a[i][d - i + 1] + a[j][d - j + 1];
if (i == j)val /= 2;
dp[now][i][j] = max(dp[now][i][j], dp[now ^ 1][x][y] + val);
}
}
}
}
for (int i = 0;i <= n;i++)
for (int j = 0;j <= n;j++)dp[now ^ 1][i][j] = -1000000000;
}
cout << dp[now][n][n];
return 0;
}
codeforces.com/problemset/problem/213/C的更多相关文章
- http://codeforces.com/problemset/problem/594/A
A. Warrior and Archer time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- http://codeforces.com/problemset/problem/712/D
D. Memory and Scores time limit per test 2 seconds memory limit per test 512 megabytes input standar ...
- http://codeforces.com/problemset/problem/847/E
E. Packmen time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- http://codeforces.com/problemset/problem/545/D
D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- codeforces 340C Tourist Problem
link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...
- codeforces B. Routine Problem 解题报告
题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
- Codeforces 527D Clique Problem
http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...
- Codeforces 706C - Hard problem - [DP]
题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
随机推荐
- OAuth的机制原理讲解及开发流程
本想前段时间就把自己通过QQ OAuth1.0.OAuth2.0协议进行验证而实现QQ登录的心得及Demo实例分享给大家,可一直很忙,今天抽点时间说下OAuth1.0协议原理,及讲解下QQ对于Oaut ...
- sqlserver 简单的事物用法
SELECT * FROM Interface_UserPort BEGIN TRY BEGIN TRAN Tran_2012_12_25 ,) --raiserror 50005N'抛出错误' CO ...
- from xml
/** * 将xml转为array * @param string $xml * @throws WxPayException */ public function FromXml($xml) { i ...
- Mixing Delphi and C++(相互调用)
Mixing Delphi and C++ You have a TStringList and <algorithm>. What can you do? Quite a lot, ac ...
- graphviz - Node Shapes
Node Shapes There are three main types of shapes : polygon-based, record-based and user-defined. The ...
- Openmpi 编译安装+集群配置 + Ubuntu14.04 + SSH无密码连接 + NFS共享文件系统
来源 http://www.open-mpi.org/ 网络连接 SSH连接,保证各台机器之间可以无密码登陆,此处不展开 hosts文件如下 #/etc/hosts 192.168.0.190 mas ...
- Selenium2学习-022-WebUI自动化实战实例-020-JavaScript 在 Selenium 自动化中的应用实例之二(获取浏览器显示区域大小)
前几篇文章中简略概述了,如何获取.设置浏览器窗口大小,那么我们该如何获取浏览器显示区域的大小呢?此文讲对此进行简略概述,敬请各位小主参阅.若有不足之处,敬请各位大神指正,不胜感激! 获取浏览器显示区域 ...
- Java学习-008-判断文件类型实例
此文源码主要为应用 Java 如何判断文件类型的源码及其测试源码.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-2 23:02:00,请知悉. Java 判断文件类型源码如 ...
- i++和++i的区别
先看如下程序: class Program { static void Main(string[] args) { ; ; ; ; x = i++; Console.WriteLine("x ...
- http://blog.csdn.net/littlechang/article/details/8642149
http://blog.csdn.net/littlechang/article/details/8642149