题干:

Frog fell into a maze. This maze is a rectangle containing NN rows and MM columns. Each grid in this maze contains a number, which is called the magic value. Frog now stays at grid (1, 1), and he wants to go to grid (N, M). For each step, he can go to either the grid right to his current location or the grid below his location. Formally, he can move from grid (x, y) to (x + 1, y) or (x, y +1), if the grid he wants to go exists. 
Frog is a perfectionist, so he'd like to find the most beautiful path. He defines the beauty of a path in the following way. Let’s denote the magic values along a path from (1, 1) to (n, m) as A1,A2,…AN+M−1A1,A2,…AN+M−1, and AavgAavg is the average value of all AiAi. The beauty of the path is (N+M–1)(N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=1(Ai−Aavg)2(N+M−1)∑i=1N+M−1(Ai−Aavg)2 
In Frog's opinion, the smaller, the better. A path with smaller beauty value is more beautiful. He asks you to help him find the most beautiful path.

Input

The first line of input contains a number TT indicating the number of test cases (T≤50T≤50). 
Each test case starts with a line containing two integers NN and MM (1≤N,M≤301≤N,M≤30). Each of the next NN lines contains MM non-negative integers, indicating the magic values. The magic values are no greater than 30.

Output

For each test case, output a single line consisting of “Case #X: Y”. XX is the test case number starting from 1. YY is the minimum beauty value.

Sample Input

1
2 2
1 2
3 4

Sample Output

Case #1: 14

题目大意:

在一个N*M的数字方格中,寻找一条从左上角(1,1)到右下角(n,m)的路径,每次只能往右走或往下走。

使得路径上的数字方差(或者说这个式子)最小。(n,m,a[i][j]均<=30)

解题报告:

如果说是方差的话更加不好思考,还是直接看上面这个式子,不难化简出:原式

然后本来想直接贪心这个式子最小的情况下转移,但是无法证明正确性。

其实我们不妨多一维状态,记录dp[i][j][k]代表从(1,1)走到(i,j)且路径的和为k的最小平方和。因为要求式子的值最小,所以在原式路径和相同的情况下被减数越小越好,又因为(i,j)不论从哪里转移过来,此时的(n+m-1)肯定为(i+j-1),是个定值,所以只需要维护平方和最小值即可。注意初始化状态的时候不是dp[0][0][0]了。。通过画二维矩阵就容易看出初始化的状态是哪些。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 55 + 5;
ll a[MAX][MAX];
ll dp[55][55][2222];
int n,m;
int main()
{int T,iCase=0;cin>>T;while(T--) {scanf("%d%d",&n,&m);int sum = 0;for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {scanf("%lld",&a[i][j]);sum += a[i][j];}}for(int i = 0; i<=n; i++) {for(int j = 0; j<=m; j++) for(int k = 0; k<=2000; k++) dp[i][j][k] = 1e12;}dp[0][1][0]=dp[1][0][0]=0;for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {for(int k = a[i][j]; k<=2000; k++) {dp[i][j][k] = min(dp[i-1][j][k-a[i][j]]+a[i][j]*a[i][j],dp[i][j-1][k-a[i][j]]+a[i][j]*a[i][j]);}} }ll ans = 0x3f3f3f3f;for(int i = 0; i<=2000; i++) {ans = min(1LL*ans,(n+m-1)*dp[n][m][i]-i*i);}printf("Case #%d: %lld\n",++iCase,ans);}return 0 ;
}
/*
1
2 2
1 2
3 4
*/

【HDU - 5492】Find a path(dp,tricks)相关推荐

  1. 【HDU - 1080】Human Gene Functions(dp,可编辑距离类问题)

    题干: 给你两个DNA序列(长度不一定相同),你可以在其中任意位置上加入空格,使得最终他俩长度相同,最终相同长度的两个DNA序列会有个相似度比较(每个字符相对应的比较),问你如何放置这些空格使得总相似 ...

  2. 【HDU - 5968】异或密码(思维,STLmap)

    题干: 晨晨在纸上写了一个长度为N的非负整数序列{aiai}.对于这个序列的一个连续子序列{al,al+1,-,aral,al+1,-,ar}晨晨可以求出其中所有数异或的结果 alxoral+1xor ...

  3. 【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化)

    题干: Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he ca ...

  4. 【CodeForces - 467C】George and Job(dp,思维)

    题干: The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, ...

  5. 【CodeForces - 574D】Bear and Blocks (dp,思维)

    题干: Limak is a little bear who loves to play. Today he is playing by destroying block towers. He bui ...

  6. 【项目实战】 图书信息管理系统(Maven,mybatis)(第一个自己独立完成的项目)

    一.实验目的 题目七 图书信息管理系统 1 功能描述 设计一个图书信息管理系统,使之具有新建图书信息.显示.插入.删除.查询和排序等功能. 2 具体设计要求 图书信息包括:图书编号.书名.作者名.出版 ...

  7. 【嵌入式AI】CNN模型压缩(剪枝,量化)详解与tensorflow实验

    1,CNN模型压缩综述 1 模型压缩的必要性及可行性 (1)必要性:首先是资源受限,其次在许多网络结构中,如VGG-16网络,参数数量1亿3千多万,占用500MB空间,需要进行309亿次浮点运算才能完 ...

  8. 【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)

    题干: Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese charac ...

  9. 【HDU 4773】Problem of Apollonius(圆的反演)

    传送门 定一个常数RRR和反演中心OOO 反演就是对于每一个点AAA变换到A′A'A′满足OA∗OA′=R2OA*OA'=R^2OA∗OA′=R2,其中A,A′,OA,A',OA,A′,O共线 对于反 ...

最新文章

  1. 图像数据增强(平移,旋转,亮度变换,翻转,添加高斯噪声,缩放,裁剪)
  2. PMcaff课堂:10年经验的产品大咖眼中的社交产品是这样的
  3. BOM 浏览器窗口尺寸 浏览器的弹出层 浏览器的地址栏 浏览器的历史记录 浏览器的版本信息 浏览器的常见事件 浏览器卷去的高度和宽度
  4. 后端在插入数据发现重复如何正确的弹出警告_前百度面试官整理的——Java后端面试题(一)...
  5. VFS之基本数据结构
  6. Hive数据更新同时去重入门
  7. android各版本源码下载
  8. 俄罗斯的程序员工资高吗
  9. html 按钮id,获取当前按钮或者html的ID名称
  10. Excel2010 count,countif,countifs使用
  11. 奇迹控制器证实配置登录器详解
  12. 关于「付费合集」的说明
  13. 鸿蒙能和ios媲美吗,把鸿蒙打造成一个和iOS相媲美的操作系统需要多久?
  14. 「多语言图像描述」最强评估基准XM3600来了!涵盖36种语言
  15. Ubuntu 20.04开启热点(连着wify开wify)
  16. 19年你在区块链行业收获了什么?
  17. 对学校的希望和寄语_家长对孩子的希望和寄语
  18. Spring MVC+Spring+Mybatis实现支付宝支付功能(图文详解+完整支付宝,微信代码)
  19. L3-2 拼题A打卡奖励 (30 分)
  20. Openzwave库中对Zwave产品配置文件的使用

热门文章

  1. 动态规划——莱文斯坦距离
  2. [Leetcode][第214题][JAVA][最短回文串][KMP][RK]
  3. [Leetcode][第332题][JAVA][重新安排行程][欧拉回路 / 欧拉通路][优先队列][DFS]
  4. html 常用字符,html 常用特殊字符
  5. java 并发 主键_高并发数据库自增主键分析
  6. 怎么让wegame适应屏幕大小_iOS的五大设计原则:统一化和适应化原则
  7. 韦东山 IMX6ULL和正点原子_GPIO和Pinctrl子系统的使用在100ASK_IMX6ULL上机实验
  8. Logistic Regression:最基础的神经网络
  9. UE4异步编程专题 - TFunction
  10. UE4 HTC VIVE - 番外篇 - 局域网联机三