HDU 5492 Find a path
Find a path
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的更多相关文章
- 2015合肥网络赛 HDU 5492 Find a path 动归
HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...
- 【动态规划】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+ ...
- 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 ...
- hdu 5492 Find a path(dp+少量数学)2015 ACM/ICPC Asia Regional Hefei Online
题意: 给出一个n*m的地图,要求从左上角(0, 0)走到右下角(n-1, m-1). 地图中每个格子中有一个值.然后根据这些值求出一个最小值. 这个最小值要这么求—— 这是我们从起点走到终点的路径, ...
- HDU - 2290 Find the Path(最短路)
HDU - 2290 Find the Path Time Limit: 5000MS Memory Limit: 64768KB 64bit IO Format: %I64d & % ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
- HDU 5492(DP) Find a path
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...
- Find a path HDU - 5492 (dp)
Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales
题目传送门 /* 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码 ...
- Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏
Base64是一种将二进制转为可打印字符的编码方法,主要用于邮件传输.Base64将64个字符(A-Z,a-z,0-9,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...
- Android Dialogs(5)[正常显示dlg,将Fragment显示为dialog,将Aty显示为dlg,嵌入],关闭Dialog
Showing a Dialog When you want to show your dialog, create an instance of your DialogFragment and ca ...
- angular.module 参数的意思
定义一个module需要两个参数,第一个作为module的名字,第二个则是指出这个module都依赖哪些别的modules
- .NET 微信开发之 获取用户数据
通过微信接口获取用户信息主要分为以下几个步骤: a.获取公众号的access_token b.通过查询所有用户OPenid接口获取所有用户. string url = "https://ap ...
- Android开发中使用数据库时出现java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
最近在开发一个 App 的时候用到了数据库,可是在使用数据库的时候就出现了一些问题,在我查询表中的一些信息时出现了一下问题: Caused by: java.lang.IllegalStateExce ...
- 架构师细说 NGINX 的配置及优化
最近感觉很多东西在运用到一定的程度之后,会发现原来是自己了解到的不够.一方面限于实际运用到的不多,一方面可能是因为一开始没有进行全面认识.遂这里搜集整理了一番NGINX. 一.nginx启动和关闭 c ...
- object -c OOP , 源码组织 ,Foundation 框架 详解1
object -c OOP , 源码组织 ,Foundation 框架 详解1 1.1 So what is OOP? OOP is a way of constructing softwar ...
- iOS-UI控件之UIImageView
contentMode属性 带有scale单词的:图片有可能会拉伸 UIViewContentModeScaleToFill 将图片拉伸至填充整个imageView 图片显示的尺寸跟imageView ...
- windows保存tomcat的控制台日志到文件
startup.bat修改:call "%EXECUTABLE%" start %CMD_LINE_ARGS%改为:call "%EXECUTABLE%" ru ...