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. BZOJ4829: [Hnoi2017]队长快跑

    BZOJ4829: [Hnoi2017]队长快跑 Description 众所周知,在P国外不远处盘踞着巨龙大Y. 传说中,在远古时代,巨龙大Y将P国的镇国之宝窃走并藏在了其巢穴中,这吸引着整个P国的 ...

  2. 我的Java开发学习之旅------>Java经典排序算法之冒泡排序

    冒泡排序(Bubble Sort)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已 ...

  3. tensorflow:typeerror:‘noneType’ object is not callable

    程序运行报错 typeerror: ‘noneType’ object is not callable 解决方法:删除缓存文件,再次运行没有错误 删除__pycache__文件夹

  4. pip安装时使用国内源加快下载速度

    国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...

  5. css 样式(checkbox开关、css按钮)

    checkbox开关 css .iosCheck { /* Blue edition */ } .iosCheck input { display: none; } .iosCheck i { dis ...

  6. openocd+jlink为mini2440调试u-boot

    需要安装openocd,如果已经安装了系统默认的openocd(默认是0.5.0,版本太低),需要先卸载掉. 在安装前需要安装必需的一些库文件: -dev libusb-1.0-0 automake ...

  7. ubuntu gitlab服务器搭建

    gitlab服务器搭建 1.安装依赖包 sudo apt-get install curl openssh-server ca-certificates postfix 执行完成后,出现邮件配置,选择 ...

  8. 简洁多用途SuperSlide插件—tab标签样式

    简洁多用途SuperSlide插件—tab标签切换代码样式,由huiyi8素材网提供. 源码:http://www.huiyi8.com/tab/

  9. k8s-创建node节点kubeconfig配置文件

    Kubeconfig 需要配置如下 TLS Bootstrapping Token kubelet kubeconfig kube-proxy kubeconfig 下载kubectl kubectl ...

  10. BZOJ_3935_Rbtree

    https://lydsy.com/JudgeOnline/problem.php?id=3935 分析: 如果知道更改后的状态,那么代价和是否合法都能求出来 对不合法的情况也设一个估价函数. 随机这 ...