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 ...
随机推荐
- Styles and Themens(4)android自定义主题时可使用的属性
A list of the standard attributes that you can use in themes can be found at R.styleable.Theme. Cons ...
- iOS 集成银联支付(绕过文档的坑,快速集成)-转
本文是投稿文章,作者:南栀倾寒当初集成支付宝的时候,觉得见了这么丑的代码,加上这么难找的下载地址,在配上几乎为零的文档,寒哥就要吐血了. 下午去集成银联,才知道血吐的早了. 下载地址:https:// ...
- android v7包的关联
最近在使用到侧滑栏的时候,使用到了v7包下的actionbar,结果折腾了好久才折腾好,其实很简单的,操作步骤如下: 1. 在eclipse中导入v7包的工程 2. 在自己的工程中打开properti ...
- Android学习笔记(八) CheckBox和RadioGroup
一.CheckBox 1.CheckBox的常用方法: boolean isChecked() :返回当前CheckBox的选中状态 void setChecked(boolean isChecked ...
- 计算机网络、OSI模型、TCP/IP族
一.计算机网络分类 1.按通信距离分类: 局域网:LAN,10m-1000m,房间.校园: 城域网:MAN,10km,城市: 广域网:WAN,100km以上,国家.全球. 二.OSI(Open Sys ...
- servlet 生命周期 与 初始化
一. 生命周期 Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy() 方法终止( ...
- python多个装饰器的执行顺序
def decorator_a(func): print 'Get in decorator_a' def inner_a(*args, **kwargs): print 'Get in inner_ ...
- cesium 原理 之 command拼接
VAO VAO(Vertext Array Object),中文是顶点数组对象.之前在<Buffer>一文中,我们介绍了Cesium如何创建VBO的过程,而VAO可以简单的认为是基于VBO ...
- 获取minist数据并转换成lmdb
caffe本身是没有数据集的,但在data目录下有获取数据的一些脚本.MNIST,一个经典的手写数字库,包含60000个训练样本和10000个测试样本,每个样本为28*28大小的黑白图片,手写数字为0 ...
- 面试之Redis
面:缓存中间件--Memcached和Redis的区别是什么? 答:Memcached的优点是简单易用,代码层次类似与Hash.支持简单数据类型,但不支持数据持久化存储,也不支持主从同步,也不支持分片 ...