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. 解读 Q_D, Q_Q 指针

    见 qglog.h文件定义: #define Q_D(Class) Class##Private * const d = d_func()    #define Q_Q(Class) Class * ...

  2. Advapi32.dll--介绍

    https://blog.csdn.net/zhoujielun123456/article/details/50338147 使用方法详见:OpsTotalService

  3. 查询oracle数据库里面所有的表名

    如果是当前用户,"select * from tab"即可

  4. 20145118 《Java程序设计》 实验报告四

    实验要求 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件处理机制: Android Studio安装 实 ...

  5. 20145302张薇《课程设计》数据恢复——WinHex实践

    20145302张薇<课程设计>数据恢复--WinHex实践 实践内容 使用WinHex破损一个U盘 使用WinHex通过DBR备份数据来修复已损坏U盘 实践详细步骤 1.准备一个文件格式 ...

  6. 20145304 Exp9 Web安全基础实践

    20145304 Exp9 Web安全基础实践 实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入是将查询语句当做查询内容输入到查询的框中,以此来使服务器执行攻击者想让它执行的语句,而不是 ...

  7. js键盘按钮keyCode及示例大全

    以功能区分布 以 keycode 编号顺序分布 keycode 0 = keycode 1 = keycode 2 = keycode 3 = keycode 4 = keycode 5 = keyc ...

  8. Linux——shell简单学习(二)

    流控制语句: for…done语句 格式:for  变量   in   名字表 do  命令列表 done 例子: #!/bin/sh for DAY in Sunday Monday Tuesday ...

  9. jenkins 工作空间的目录

    /usr/share/tomcat7/.jenkins/workspace/

  10. Use a layout_width of 0dip instead of fill_parent for better performance

    安装了最新的ATD 18之后,新加的Lint Warnings插件会给我们检测出许多xml布局中不当的地方,例如: Use a layout_width of 0dip instead of fill ...