hdu-5492 Find a path(dp)
题目链接:
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
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.
#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)的更多相关文章
- 2015合肥网络赛 HDU 5492 Find a path 动归
HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...
- 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 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- HDU - 2290 Find the Path(最短路)
HDU - 2290 Find the Path Time Limit: 5000MS Memory Limit: 64768KB 64bit IO Format: %I64d & % ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- EventRay UI Kit – Web & Mobile 的素材
EventRay UI 工具包是一个免费的,可以现成使用的界面套件.包括多个为 Web 和移动应用设计的布局和 UI 元素.所有你需要做的就是下载这个 UI 工具包,点击源码下载打开的页面即可下载. ...
- #8.11.16总结#CSS常用样式总结(二)
border 边框 简写:border:1px solid #000; 等效于:border-width:1px;border-style:solid;border-color:#000; 顺序:b ...
- .Net框架2.0和4.0版本对比
.Net版本 2.0 SP2 4.0 操作系统 Windows 2000 SP4以上 Windows XP SP3以上 安装包大小 NetFx20SP2_x86.exe 23.8 MBNetFx20S ...
- sharepoint 权限继承相关
重新继承父级权限: SPList.ResetRoleInheritance(); 断开继承: SPList.BreakRoleInheritance(true); 用powershell断开继承: $ ...
- Tomcat https自制证书和浏览器配置
Tomcat配置成https后,如过使用的是自己的证书,登陆首页时,总是提示证书安全问题,网上的很多资料有描述,但比较复杂,找了几个配置不成功,现在描述一个比较简单的方法. 生成证书的脚本 #!/bi ...
- iOS 设置系统屏幕亮度
// 设置系统屏幕亮度 // [UIScreen mainScreen].brightness = value; // 或者 [[UIScreen mainScreen] se ...
- Android ListView添加多种类型的ItemView
一般复杂的ListView都会重写BaseAdapter,通过重用convertView来减少inflate,通过setTag()和ViewHolder改变ItemView的内容. 重写BaseAda ...
- 【读书笔记】iOS网络-异步请求与运行循环
异步请求需要运行循环.当数据传递到服务器或是被客户端接收时,运行循环用于实现事件与委托对象之间的通信.异步请求在发出时,会在当前线程的运行循环上操作,这个实现细节是很重要的,因为在GCD块中或者是通过 ...
- iOS中block的使用、实现底层、循环引用、存储位置
一.整体介绍 定义:C语言的匿名函数,
- TFS2012 自动生成与部署
思路: 每日构建,自动生成,然后从TFS提交日志中提取版本修订说明,调用打包脚本混淆并生成安装包(系统自带的太锉),最后将相关文件复制到指定网站供浏览下载.自动向测试网站发布,自动生成数据库并初始化. ...