Find a path

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

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
 
Source
 
 
  求方格迷宫里的最小方差路径。
  化简式子:    
  

可以看出问题就是要使得所有的(N+M-1)*(A平方的和)减去所有A的和的平方达到最小。

令f[i][j][k]表示走到(i,j)处,且当前走过的格子的法力值的和为k(即SUM{A}=k)的时候的最小的SUM{Ai^2}的值。

最后答案就是MIN{ f[i][j][k]*(N+M-1)-k*k }

 #include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int f[][][];
int e[][];
int main(){
int N,M,T,i,j,k;
cin>>T;
for(int cas=;cas<=T;++cas){
cin>>N>>M;
for(i=;i<=N;++i){
for(j=;j<=M;++j){
cin>>e[i][j];
}
}
memset(f,inf,sizeof(f));
f[][][e[][]]=e[][]*e[][];
for(i=;i<=N;++i){
for(j=;j<=M;++j){
for(k=;k<;++k){
if(f[i][j][k]!=inf){
f[i][j+][k+e[i][j+]]=min(f[i][j+][k+e[i][j+]],f[i][j][k]+e[i][j+]*e[i][j+]);
f[i+][j][k+e[i+][j]]=min(f[i+][j][k+e[i+][j]],f[i][j][k]+e[i+][j]*e[i+][j]);
}
}
}
}
int ans=inf;
for(i=;i<;++i){
if(f[N][M][i]!=inf){
ans=min(ans,f[N][M][i]*(N+M-)-i*i);
}
}
cout<<"Case #"<<cas<<": "<<ans<<endl;
}
return ;
}

hdu-5492-dp的更多相关文章

  1. HDU 5492(DP) Find a path

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...

  2. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

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

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

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

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

  6. Find a path HDU - 5492 (dp)

    Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. 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 ...

  8. HDU 5492 Find a path

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

  9. HDU 1069 dp最长递增子序列

    B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  10. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. Ubuntu系统下Jenkins的本地构建基本方法

    上一篇文章介绍了,jenkins的安装和系统配置之后,配置登录成功后,就可以新建jenkins构建项目,用于自动化构建. 1.项目名称和项目描述 点击左上角的 新建任务,输入项目名称,选择 构建一个自 ...

  2. Django框架----models.py(数据库操作文件)

    利用一个中间模块 帮助我们连接数据库,写SQL语句,执行SQL语句,拿到结果 models.py 一.数据的对应关系 1. 类 ---> 表 2. 对象 ---> 数据行 3. 属性 -- ...

  3. Centos下使用php调用shell脚本

    我们在实际项目中或许会遇到php调用shell脚本的需求.下面就用简单案例在Centos环境下实践 准备 查看php.ini中配置是否打开安全模式 //php.ini safe_mode = //这个 ...

  4. php检查是否是数字和字母

    /* 检查是否是数字和字母* php内置函数ctype_alnum检查字符串是否是数字和字母,或者两者混合* $string*/ public function is_numandlitter($st ...

  5. Python3 判断文件和文件夹是否存在、创建文件夹

    Python3 判断文件和文件夹是否存在.创建文件夹 python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件: 1) os.mknod(“test.txt”) 创建空文件  ...

  6. linux查看是否能访问外网及拥有的公网IP

    linux查看是否能访问外网及拥有的公网IP linux查看是否能访问外网及拥有的公网IP: 1,测访问外网能力:curl -l http://www.baidu.com 2,测访问外网能力:wget ...

  7. 02: 安装epel 解决centos7无法使用yum安装nginx

    参考网址: http://www.mamicode.com/info-detail-1671603.html 1.yum命令安装 yum install epel-release -y 2.更新数据 ...

  8. zabbix zabbix_agentd.conf详解

    # This is a config file for the Zabbix agent daemon (Unix) # To get more information about Zabbix, v ...

  9. C#中值和引用

    c#中有两种基本类型,它们分别是值类型和引用类型:而每种类型都可以细分为如下类型: ps:1.基本类型是值类型 2.类.接口.委托都是引用类型

  10. linux下使用docker-thunder-xware进行离线下载

    1.环境: lsb_release -a hello@jhello:~$ lsb_release -aNo LSB modules are available.Distributor ID: Ubun ...