题目链接:

Find a path

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1557    Accepted Submission(s): 678

Problem Description
Frog fell into a maze. This maze is a rectangle containing N rows and M 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−1, and Aavg is the average value of all Ai. The beauty of the path is (N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=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 T indicating the number of test cases (T≤50).
Each test case starts with a line containing two integers N and M (1≤N,M≤30). Each of the next N lines contains M 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”. X is the test case number starting from 1. Y is the minimum beauty value.
 
Sample Input
1
2 2
1 2
3 4
 
Sample Output
Case #1: 14
 
题意:
 
找一条从(1,1)到(n,m)的路径使得上面那个式子的值最小;
 
思路:
 
最后那个式子可以化简成(n+m-1)*∑Ai2-(∑Ai)2;
dp[i][j][k]表示到达(i,j)时和为k的平方和;转移就好转移了,结果也可以表示出来了;
有时候不能直接用dp某某表达,所以需要转换一下思路;
 
AC代码:
 
#include <bits/stdc++.h>
using namespace std;
const int inf=1e7;
const int maxn=1e5+10;
int n,m,a[40][40];
int dp[32][32][61*31];
int QAQ(int x){return x*x;}
int main()
{
int t,Case=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
int hi=(n+m)*31;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
for(int k=0;k<=hi;k++)
dp[i][j][k]=inf;
dp[1][1][a[1][1]]=QAQ(a[1][1]);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int k=0;k<=hi;k++)
{
dp[i][j+1][k+a[i][j+1]]=min(dp[i][j+1][k+a[i][j+1]],dp[i][j][k]+QAQ(a[i][j+1]));
dp[i+1][j][k+a[i+1][j]]=min(dp[i+1][j][k+a[i+1][j]],dp[i][j][k]+QAQ(a[i+1][j]));
}
}
}
int ans=inf;
for(int i=0;i<=hi;i++)ans=min(ans,(n+m-1)*dp[n][m][i]-QAQ(i));
printf("Case #%d: %d\n",++Case,ans);
}
return 0;
}

  

  

hdu-5492 Find a path(dp)的更多相关文章

  1. 2015合肥网络赛 HDU 5492 Find a path 动归

    HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...

  2. HDU - 5492 Find a path(方差公式+dp)

    Find a path Frog fell into a maze. This maze is a rectangle containing NN rows and MM columns. Each ...

  3. hdu 5492 Find a path(dp+少量数学)2015 ACM/ICPC Asia Regional Hefei Online

    题意: 给出一个n*m的地图,要求从左上角(0, 0)走到右下角(n-1, m-1). 地图中每个格子中有一个值.然后根据这些值求出一个最小值. 这个最小值要这么求—— 这是我们从起点走到终点的路径, ...

  4. HDU 5492 Find a path

    Find a path Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...

  5. 【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...

  6. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  7. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  8. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  9. HDU - 2290 Find the Path(最短路)

    HDU - 2290 Find the Path Time Limit: 5000MS   Memory Limit: 64768KB   64bit IO Format: %I64d & % ...

  10. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. 【HTML5】Canvas图像

    把一幅图像放置到画布上, 使用以下方法: drawImage(Img,x,y); 注:这里的Img必须是一个图像对象.   显示一个canvas图像: <!DOCTYPE html> &l ...

  2. javascript --- 原型初探七日谈(一)

    在javascript中,像原型,闭包这样的概念,只要我们能领悟其中的原理,一切都会显得格外清晰与明了. 原型属性(prototype): 下面我们简单定义一个函数 function her(a, b ...

  3. loading插件(原创)

    前言:服务器这几天都连不上,所以迟迟未更新,今天连上后才把插件文件和文档上传了.良心之作啊,难度虽不高,但命名多文件多啊.我得马上写篇博客絮叨一下,直接上地址. 文档及下载地址:www.chenggu ...

  4. Office 365 - SharePoint 2013 Online之添加App开发工具Napa

    1.新建一个网站集,模板选择开发人员模板,如下图: 2.确定以后,需要稍等一会儿; 3.点击网站内容,添加app,如下图: 4.进入SharePoint Store,选择Napa,如下图: 5.选择A ...

  5. DevExpress 2015.2发布 看看有哪些更新

    下面是besy翻译的部分重要更新,要查看全部更新细节请访问英文官网. | 下载DevExpress 2015.2 Diagram Control 新的DevExpress Diagram Contro ...

  6. JavaScript学习03 JS函数

    JavaScript学习03 JS函数 函数就是包裹在花括号中的代码块,前面使用了关键词function: function functionName() { 这里是要执行的代码 } 函数参数 函数的 ...

  7. android 界面设计基本知识Ⅱ

    上一章讲述了Android界面设计时,一些基本控件的使用,本章主要讲述自定义控件,Fragment和Headler线程机制. 1.自定义控件 (1)基本知识 dp.sp和dx      px:像素点  ...

  8. 创建用户故事地图(User Story Mapping)的8个步骤

    [小编]上周六了解了用户故事地图后,小编又查阅了一些资料,找到了以下这篇关于如何组织用户故事地图规划的文章,分享给大家.也希望大家如果有好的实践,也可以留言一起交流. 原文地址:http://winn ...

  9. JS怎么动态命名变量名

    [摘要]本文是对JS怎么动态命名变量名的讲解,对学习JavaScript编程技术有所帮助,与大家分享. 1.用eval,例子: 1 2 3 4 5 6 7 <script> var Thr ...

  10. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...