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: ...
随机推荐
- linux内核学习之一:环境搭建--安装Debian7.3
本系列文章假设读者已对linux有一定的了解,其实学习linux内核不需要有很深的关于linux的知识,只需要了解以下内容:linux基础知识及基本shell命令:现代操作系统的基本概念:C语言和gc ...
- 创建区域Areas,添加TagHelper
创建区域Areas,添加TagHelper 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在Asp.net Core VS2015中,我们发现还有很多不太简便的地方,比如右 ...
- JQuery的插件
最近需要修改ftl文件,使用一般的freemarker插件不能有效处理里边的部分JQuery内容,所以特地下载了一个Spket插件, 地址为 http://www.agpad.com/update 再 ...
- 利用php unpack读取c struct的二进制数据,struct内存对齐引起的一些问题
c语言代码 #include <stdio.h> struct test{ int a; unsigned char b; int c; }; int main(){ FILE *fp; ...
- 2015第24周三Spring事务3
在一个典型的事务处理场景中,有以下几个参与者: Resource Manager(RM) ResourceManager简称RM,它负责存储并管理系统数据资源的状态,比如数据库服务器,JMS消息服务器 ...
- UIView添加支持代码块的手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a ...
- 关于打开Eclipse时出现eclipse failed to create the java virtual machine与locking is not possible in the direc
原文转自:http://www.cnblogs.com/steararre/p/4037453.html 今天在机子上使用Eclipse时候打开发现这两个问题,通过查阅资料膜拜大神博客得知解决方法,特 ...
- hdu 4586 Play the Dice(概率dp)
Problem Description There is a dice with n sides, which are numbered from 1,2,...,n and have the equ ...
- Cuts the cake_hdu_2134.java
Cuts the cake Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- [AngularJS] Transforming raw JSON data to meaningful output in AngularJS
angular.module('APP', []) .controller('MainController', function($scope, UserConstants){ var user = ...