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. 按模板导出Excel

    说明:开发环境 vs2012 asp.net mvc4 c# 注意:Excel模板有多个sheet页,导出Excel的时候,同时给多个sheet页填充数据 1.项目结构 3.Excel模板(注意she ...

  2. linux-shell脚本命令之grep

    版权声明: https://blog.csdn.net/zdp072/article/details/26015611 [ grep简单介绍: ] grep是用来过滤含有特定字符的行, 能使用正則表達 ...

  3. redux和mobx比较(一)

    Redux vs Mobx 那么具体到这两种模型,又有一些特定的优缺点呈现出来,先谈谈 Redux 的优势: 数据流流动很自然,因为任何 dispatch 都会导致广播,需要依据对象引用是否变化来控制 ...

  4. Java for LeetCode 114 Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...

  5. ruby 精选网站

    ruby 基础   http://www.yiibai.com/ruby/2013/0820174.html     http://www.rubydoc.info/github       http ...

  6. 【LeetCode-easy】Merge Two Sorted Lists

    思路:指针p用于串联怎个链表,比较两个指针的大小,连接较小的一个.如果一个链表到达链尾,连接另外一个链表余下来的所以节点. public ListNode mergeTwoLists(ListNode ...

  7. 开始Shell编程

    开始Shell编程 NT:如无特别说明,下面使用bash shell. 编写脚本只需以下几步: (1) 打开编辑器,写下脚本. (2) 给保存的脚本执行权限. 使用chmod permission y ...

  8. Git 使用初步

    官网:https://git-scm.com/ 官方文档:https://git-scm.com/doc 比较简略的资料(对基本概念没有解释很清楚):http://wenku.baidu.com/li ...

  9. apace搭建站点

    Listen 127.0.0.1:3310<VirtualHost *:3306> ServerName 127.0.0.1:3306 DocumentRoot "F:/Baid ...

  10. TEE&TrustZone

    一.TEE(Trusted Execution Environment) 1 A look back 1)2009 OMTP(Open Mobile Terminal Platform),首次定义了T ...