Find a path

Frog fell into a maze. This maze is a rectangle containing NN rows and MM 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−1A1,A2,…AN+M−1, and AavgAavg is the average value of all AiAi. The beauty of the path is (N+M–1)(N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=1(Ai−Aavg)2(N+M−1)∑i=1N+M−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. 

InputThe first line of input contains a number TT indicating the number of test cases (T≤50T≤50). 
Each test case starts with a line containing two integers NN and MM (1≤N,M≤301≤N,M≤30). Each of the next NN lines contains MM non-negative integers, indicating the magic values. The magic values are no greater than 30. 
OutputFor each test case, output a single line consisting of “Case #X: Y”. XX is the test case number starting from 1. YY is the minimum beauty value.Sample Input

1
2 2
1 2
3 4

Sample Output

Case #1: 14

将公式变形得:(n+m-1)*ΣAi^2-(ΣAi)^2
dp求出每种和的最小的平方和,最后找出满足公式的最小解。
#include<bits/stdc++.h>
#define MAX 31
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int a[MAX][MAX];
int dp[MAX][MAX][]; int main()
{
int tt=,t,n,m,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;i++){
for(j=;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
memset(dp,INF,sizeof(dp));
dp[][][]=;dp[][][]=;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
for(k=;k<=;k++){
if(k+a[i][j]<=) dp[i][j][k+a[i][j]]=min(dp[i][j][k+a[i][j]],min(dp[i-][j][k],dp[i][j-][k])+a[i][j]*a[i][j]);
}
}
}
int ans=INF;
for(i=;i<=;i++){
if(dp[n][m][i]!=INF){
ans=min(ans,(n+m-)*dp[n][m][i]-i*i);
}
}
printf("Case #%d: %d\n",++tt,ans);
}
return ;
}

HDU - 5492 Find a path(方差公式+dp)的更多相关文章

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

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

  2. HDU 5492 Find a path

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

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

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

  4. 【动态规划】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+ ...

  5. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  6. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. HDU - 2290 Find the Path(最短路)

    HDU - 2290 Find the Path Time Limit: 5000MS   Memory Limit: 64768KB   64bit IO Format: %I64d & % ...

  8. HDU 3341 Lost's revenge AC自动机+dp

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  9. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

随机推荐

  1. JavaScript 如何创建search字段

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Django 初始化数据库

    django 初始化数据库 刷新数据库guoguos-MacBook-Pro:mysite guoguo$ python manage.py sqlflushBEGIN;SET FOREIGN_KEY ...

  3. UITableViewCell的多选操作

    版权声明:本文为博主原创文章.未经博主同意不得转载,转载需加上原博客链接. https://blog.csdn.net/panyong4627/article/details/37902207 - ( ...

  4. Light OJ 1005 - Rooks 数学题解

    版权声明:本文作者靖心.靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  5. Servlet详解(转)

    我们通过浏览器访问一个网页的过程,实际上是浏览器(例如IE)通过HTTP协议(参见附录B)和Web服务器(也叫做HTTP服务器)进行交互的过程. 也就是说,用户要访问网络资源,首先需要在网络上架设We ...

  6. SIFT算法详解(转)

    原文地址 http://blog.csdn.net/pi9nc/article/details/23302075 尺度不变特征变换匹配算法详解 Scale Invariant Feature Tran ...

  7. [数据挖掘课程笔记]无监督学习——聚类(clustering)

    什么是聚类(clustering) 个人理解:聚类就是将大量无标签的记录,根据它们的特点把它们分成簇,最后结果应当是相同簇之间相似性要尽可能大,不同簇之间相似性要尽可能小. 聚类方法的分类如下图所示: ...

  8. Java多线程系列 基础篇10 wait/notify/sleep/yield/join

    1.Object类中的wait()/notify()/notifyAll() wait(): 让当前线程处于Waiting状态并释放掉持有的对象锁,直到其他线程调用此对象的线程notify()/not ...

  9. Linux学习之路(四)帮助命令

    帮助命令man .man 命令 #获取指定命令的帮助 .man ls #查看ls的帮助 man的级别 1 查看命令的帮助 2 查看可被内核调用的函数的帮助 3 查看函数的函数库的帮助 4 查看特殊文件 ...

  10. BZOJ(begin) 1328 [Usaco2003 Open]Jumping Cows:贪心【波峰波谷模型】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1328 题意: 给你一个长度为n的正整数序列. 可以选任意个数字,只能从左往右选. 偶数 ...