Find a path

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 5492
64-bit integer IO format: %I64d      Java class name: Main

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 $A_1,A_2,…A_{N+M−1}$, and $A_{avg}$ is the average value of all $A_i$. The beauty of the path is (N+M–1) multiplies the variance of the values:$(N+M−1)\sum_{i=1}^{N+M−1}(A_i−A_{avg})^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\leq 50).$
Each test case starts with a line containing two integers N and M $(1\leq N,M\leq 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
2015 ACM/ICPC Asia Regional Hefei Online

解题:动态规划,最小方差路

$dp[i][j][k]表示在(i,j)格子中\sum{A_i}为k的时候最小的\sum{A_i^2}$

 #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int mp[maxn][maxn],dp[maxn][maxn][];
int main(){
int kase,n,m,cs = ;
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&n,&m);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j)
scanf("%d",mp[i] + j);
memset(dp,0x3f,sizeof dp);
dp[][][] = dp[][][] = ;
for(int i = ; i <= n; ++i){
for(int j = ; j <= m; ++j){
for(int k = ; k <= ; ++k){
if(dp[i-][j][k] != INF)
dp[i][j][k + mp[i][j]] = min(dp[i][j][k + mp[i][j]],dp[i-][j][k] + mp[i][j]*mp[i][j]);
if(dp[i][j-][k] != INF)
dp[i][j][k + mp[i][j]] = min(dp[i][j][k + mp[i][j]],dp[i][j-][k] + mp[i][j]*mp[i][j]);
}
}
}
int ret = INF;
for(int i = ; i <= ; ++i)
if(dp[n][m][i] < INF) ret = min(ret,(n + m - )*dp[n][m][i] - i*i);
printf("Case #%d: %d\n",cs++,ret);
}
return ;
}

HDU 5492 Find a path的更多相关文章

  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 (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...

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

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

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

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

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

  6. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  7. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

  8. HDU 5492(DP) Find a path

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

  9. Find a path HDU - 5492 (dp)

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

随机推荐

  1. [PKUWC2018]猎人杀

    题解 感觉是一道神题,想不出来 问最后\(1\)号猎人存活的概率 发现根本没法记录状态 每次转移的分母也都不一样 可以考虑这样一件事情: 如果一个人被打中了 那么不急于从所有人中将ta删除,而是给ta ...

  2. [USACO 2012 Feb Gold] Cow Coupons【贪心 堆】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=118 传送门2:http://www.lydsy.com/JudgeOn ...

  3. (转)C语言运算符优先级 详细列表

    C语言运算符优先级 详细列表 文章转自:Slyar Home 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右   () 圆括号 (表达式)/函数 ...

  4. 波哥!一个不安分的IT男

    第一篇博文,紧张,窃喜,辣眼睛! 这个订阅号主要是写给自己的,近期越来越发现记忆力不如以前了! 时光如梭,岁月荏苒,或许这两句经典的开头文比较契合自己的年纪.依稀记得几年前还在组装服务器.搬机柜.做系 ...

  5. jvm 脑图

  6. openID 无效

    1.appid 和秘钥一定要是你目前正在测试公众号的数据,如果 appid 和 秘钥是测试账号的,而目标测试业务是在正式的公众号,及时能取到acces——token ,也会报无效的openid 遇到的 ...

  7. Node.js——事件与发布机制

  8. Asp.Net 设计模式 之 “特殊”的单例模式

    特殊的单例模式 要点在这里,提前预览: public SingleDemo() { name = "yy"; age = 20; //特殊的单例,this指代得失当前的Single ...

  9. 迅为电子iTOP-HMI043 4.3寸人机界面产品

    4.3寸人机界面: 7寸人机界面: 10.2寸人机界面: 产品认证CE:符合EN61000-6-2:2005, EN61000-6-4:2007标准FCC 兼容性:符合FCC Class A面板防护等 ...

  10. ansible配置mysql主从复制

    配置主机1.下载安装所需安装包 [root@server1 ansible]# lsansible-2.7.8-1.el7.noarch.rpmansible-tower-setup-bundle-3 ...