题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492

题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是这些数的方差最小。

Find a path

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 961    Accepted Submission(s): 431

Problem Description
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 A1,A2,…AN+M−1, and Aavg is the average value of all Ai. The beauty of the path is (N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=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.
 
Input
The first line of input contains a number T indicating the number of test cases (T≤50).
Each test case starts with a line containing two integers N and M (1≤N,M≤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
 #include <iostream>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std; int mi(int x,int y)
{
if (x<y)
return x;
else
return y;
}
int ans,t,n,m,dp[][][],a[][];
int main()
{ cin>>t;
for (int cas=;cas<=t;cas++)
{
cin>>n>>m;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
cin>>a[i][j]; memset(dp,inf,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+a[i][j]]=mi(dp[i][j][k+a[i][j]],dp[i-][j][k]+a[i][j]*a[i][j]);
}
if (dp[i][j-][k]!=inf)
{
dp[i][j][k+a[i][j]]=mi(dp[i][j][k+a[i][j]],dp[i][j-][k]+a[i][j]*a[i][j]);
}
}
}
}
ans=inf;
for (int i=;i<=;i++)
{
if (dp[n][m][i]!=inf)
ans=mi(ans,dp[n][m][i]*(n+m-)-i*i);
}
cout <<"Case #"<<cas<<": "<<ans<<endl;
}
return ;
}
 

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

  1. 2015合肥网络赛 HDU 5492 Find a path 动归

    HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...

  2. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

  4. Find a path HDU - 5492 (dp)

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

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

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

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

  7. HDU 5492 Find a path

    Find a path Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...

  8. 【动态规划】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+ ...

  9. HDU 1069&&HDU 1087 (DP 最长序列之和)

    H - Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

随机推荐

  1. 从事web前端的这些日子

    不知不觉从事web前端快要一年了,在这一年的时间,自己的技术也得到了不小的进步,但毕竟还是停留在摸索的阶段,前端的这条路还有很长的路要走,前端要掌握的东西太多,知识也在频繁的更新.每天在群里和别人的交 ...

  2. [POJ] 1274 The Perfect Stall(二分图最大匹配)

    题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...

  3. LeetCode_Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  4. VMware Network Adapter VMnet1和VMnet8 未识别的网络的解决方法

    VMware Network Adapter VMnet1和VMnet8 被防火墙认定为未识别的网络,阻隔,无法使用端口映射,虚拟机的80端口无法传入,数据包只能出不能入.且公用网络被限制不能修改为家 ...

  5. Android String 转 MD5

    /** * 将字符串转成16 位MD5值 *  * @param string * @return */ public static String MD5(String string) { byte[ ...

  6. mysql sql优化<1>

    <pre name="code" class="html">explain SELECT t.* FROM ( SELECT t1.sn AS cl ...

  7. 【转】如何过滤 adb logcat 输出

    原文网址:http://www.cnblogs.com/imouto/archive/2012/12/11/filtering-adb-logcat-output.html 简介: 本文介绍如何在 s ...

  8. puppet foreman

    服务器(物理机器和虚拟机)多了以后需要工具来管理,经常登陆系统后不知是在虚拟机上还是在物理机上?如果在虚拟机上这个虚拟机运行在哪个服务器节 点(host)上?如果在物理机上运行在什么配置的物理机上?运 ...

  9. c++之命名空间namespace

    1命名空间解决全局变量的冲突 main.h文件 #pragma once // data命名空间的名称 namespace data { ;//外部全局变量冲突 } main.cpp #include ...

  10. c语言结构体5之匿名结构体

    注意: 1匿名结构体不会出现重合 重命名的情况 2有名结构体 名称不能相同 也就是不能重名 //匿名结构体不会出现重名的情况 struct //无名结构体 { ]; ]; int num; };//不 ...