HDU 5492(DP) Find a path
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492
题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是这些数的方差最小。
Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 961 Accepted Submission(s): 431
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.
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.
2 2
1 2
3 4
#include <iostream>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std; int mi(int x,int y)
{
if (x<y)
return x;
else
return y;
}
int ans,t,n,m,dp[][][],a[][];
int main()
{ cin>>t;
for (int cas=;cas<=t;cas++)
{
cin>>n>>m;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
cin>>a[i][j]; memset(dp,inf,sizeof(dp));
dp[][][]=;
dp[][][]=; for (int i=;i<=n;i++)
{
for (int j=;j<=m;j++)
{
for (int k=;k<=;k++)
{
if (dp[i-][j][k]!=inf)
{
dp[i][j][k+a[i][j]]=mi(dp[i][j][k+a[i][j]],dp[i-][j][k]+a[i][j]*a[i][j]);
}
if (dp[i][j-][k]!=inf)
{
dp[i][j][k+a[i][j]]=mi(dp[i][j][k+a[i][j]],dp[i][j-][k]+a[i][j]*a[i][j]);
}
}
}
}
ans=inf;
for (int i=;i<=;i++)
{
if (dp[n][m][i]!=inf)
ans=mi(ans,dp[n][m][i]*(n+m-)-i*i);
}
cout <<"Case #"<<cas<<": "<<ans<<endl;
}
return ;
}
HDU 5492(DP) Find a path的更多相关文章
- 2015合肥网络赛 HDU 5492 Find a path 动归
HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5928 DP 凸包graham
给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
- Find a path HDU - 5492 (dp)
Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 ...
- hdu 5492 Find a path(dp+少量数学)2015 ACM/ICPC Asia Regional Hefei Online
题意: 给出一个n*m的地图,要求从左上角(0, 0)走到右下角(n-1, m-1). 地图中每个格子中有一个值.然后根据这些值求出一个最小值. 这个最小值要这么求—— 这是我们从起点走到终点的路径, ...
- HDU 5492 Find a path
Find a path Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...
- 【动态规划】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+ ...
- HDU 1069&&HDU 1087 (DP 最长序列之和)
H - Super Jumping! Jumping! Jumping! Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
随机推荐
- AndroidStudio push代码到github
步骤 1 studio的git配置: 安装好git后启动Androidstudio,打开如下路径File->Settings->Version Control(展开)->git 在P ...
- git强制更新
1.下载远程的库的内容 git fetch --all 2.把HEAD指向刚刚下载的最新的版本 git reset --hard origin/master
- Express4 Route笔记
可以参考Express官网关于路由一节:http://expressjs.com/guide/routing.html 1:通过使用GET.POST方式定义主页路由,app.js: var expre ...
- hdu 畅通工程再续
http://acm.hdu.edu.cn/showproblem.php?pid=1875 #include <cstdio> #include <cstring> #inc ...
- 伟福与Keil的比较--51汇编提高篇
[写在前面] 本文适合有一定汇编水平的人(了解大半的汇编语句,能区分全角与半角符号,能够独立编写流水灯.数码管等程序),传授51单片机的汇编语言经验.如果您发现不少指令不知道意思,请从网上搜索入门教程 ...
- BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 327 Solved: ...
- BZOJ1628: [Usaco2007 Demo]City skyline
1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 256 Solved: 210[Su ...
- MVC4.0 Controller和View重复加载
项目完成以后总是感觉有一些页面跑起来特别的慢,就仔细的调试了下,发现有很多也买年都是走了两遍,页面加载的时候Controller和View会连续走了两次,没有一点缘由 查了很久也不知道什么原因,这个问 ...
- Go语言的构建方法总结
趁着近期要换工作的空闲时间,看了一下Go语言,与C++相比,Go语言的确在不少地方轻便了不少,例如:增加了内置的字符串类型.多个返回值.支持协程.简单的构建方法等等.使得在生产效率方面有了不少的提高. ...
- apache2 httpd 基于域名的虚拟主机配置 for centos6X 和debian-8
全系统虚拟主机: for debian 系统的apache2 域名 虚拟主机