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: ...
随机推荐
- Centos common software install
1.本地安装soft yum localinstall xxx.rpm 2.kolourpaintyum install kolourpaint
- 《VIM-Adventures攻略》前言
本文已转至http://cn.abnerchou.me/2014/03/02/bfdaadb0/ 自从有了计算机,人们就想向其灌输自己的想法. 要想对其输入,自然离不开文本编辑器. 公告:<VI ...
- cf C. Insertion Sort
http://codeforces.com/contest/362/problem/C #include <cstdio> #include <cstring> #includ ...
- Linux下查看文件夹或目录大小
当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. du可以查看文件及文件夹的大小.d ...
- CH Round #40 - Fimbulvetr::Prologue
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2340%20-%20Fimbulvetr%3A%3APrologue/起源 题意:树上最大流+费用流,源点为树根 ...
- cf445B DZY Loves Chemistry
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 教程:30分钟学会Adobe Premiere
原文地址:http://tieba.baidu.com/p/2785313831 视频教程地址
- Windows系统结构
四种用户模式进程:1.系统支持进程,比如登录进程和会话管理器,并不是Windows服务,不有服务控制管理器启动2.服务进程,一些以Windows服务方式来运行的组件3.用户应用进程4.环境子系统服务器 ...
- 区间DP(初步了解)
区间动态规划问题一般都是考虑.对于每段区间,他们的最优值都 是由几段更小区间的最优值得到,是分治思想的一种应用,将一个区间 问题不断划分更小的区间直至一个元素组成的区间,枚举他们的组合 .求合并后的 ...
- Dynamics CRM 常用 C# 方法集合
Plugin(C#) 分派 AssignRequest assign = new AssignRequest(); assign.Assignee = prEntity["ownerid&q ...