Find a path

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

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
 
Source
 
Recommend
wange2014
 
题意:

给出一个n*m的地图,要求从左上角(0, 0)走到右下角(n-1, m-1)。

地图中每个格子中有一个值。然后根据这些值求出一个最小值。

这个最小值要这么求——

这是我们从起点走到终点的路径,其中N是地图的长,M是地图的宽,Ai表示路径中第i个点的值,Aavg表示路径中所有的点的值的平均值。要求这个式子的值最小。

我们可以将它转化为

好了,推到这里,我们需要的数学知识就结束了(实际上以我的数学知识也只能做到这里了……)。然后dp就好了——

Dp[i][j][k],i表示第i行,j表示第j列,k表示所有Ai的和,这个里面保存的是所有 的值。

然后dp下去就好了.

dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k-mp[i-1][j]]+mp[i][j]*mp[i][j], dp[i][j][k-mp[i][j-1]]+mp[i][j]*mp[i][j]);

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define _e exp(1.0)
#define ll long long
const int maxn=; int t,n,m;
int dp[maxn][maxn][],map[maxn][maxn];
int ans; int minn(int x,int y)
{
if(x==-)
return y;
return x<y?x:y;
}
void solve()
{
memset(dp,-,sizeof(dp));
dp[][][map[][]]=map[][]*map[][];
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(i->=)
for(int k=map[i][j];k<;k++)
if(dp[i-][j][k-map[i][j]]!=-)
dp[i][j][k]=minn(dp[i][j][k],dp[i-][j][k-map[i][j]]+map[i][j]*map[i][j]);
if(j->=)
for(int k=map[i][j];k<;k++)
if(dp[i][j-][k-map[i][j]]!=-)
dp[i][j][k]=minn(dp[i][j][k],dp[i][j-][k-map[i][j]]+map[i][j]*map[i][j]);
}
}
int main()
{
scanf("%d",&t);
int ca=;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
scanf("%d",&map[i][j]);
solve();
ans=-;
for(int i=;i<;i++)
if(dp[n-][m-][i]!=-)
{
int mid=(n+m-)*dp[n-][m-][i]-i*i; //把化简后的公式里的剩余部分补齐
if(ans==-)
ans=mid;
else
ans=ans<mid?ans:mid;
}
printf("Case #%d: %d\n",ca++,ans);
}
return ; }

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

  1. hdu 5534(dp)

    Input The first line contains an integer T indicating the total number of test cases. Each test case ...

  2. HDU 5800 (DP)

    Problem To My Girlfriend (HDU 5800) 题目大意 给定一个由n个元素组成的序列,和s (n<=1000,s<=1000) 求 :   f (i,j,k,l, ...

  3. hdu 5464(dp)

    题意: 给你n个数,要求选一些数(可以不选),把它们加起来,使得和恰好是p的倍数(0也是p的倍数),求方案数. - - 心好痛,又没想到动规 #include <stdio.h> #inc ...

  4. HDU 2571(dp)题解

    命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  5. 饭卡 HDU - 2546(dp)

    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...

  6. HDU 4489(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4489 解题思路这里已经说的很清楚了: http://blog.csdn.net/bossup/article/d ...

  7. hdu 1024(dp)

    传送门:Max Sum Plus Plus 题意:从n个数中选出m段不相交的连续子段,求这个和最大. 分析:经典dp,dp[i][j][0]表示不取第i个数且前i个数分成j段达到的最优值,dp[i][ ...

  8. AreYouBusy HDU - 3535 (dp)

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

  9. ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/d ...

随机推荐

  1. 引用 Session详解 作者:郎云鹏

    本文转载自leeldy<Session详解 作者:郎云鹏>   引用 leeldy 的 Session详解 作者:郎云鹏 目录: 一.术语session 二.HTTP协议与状态保持 三.理 ...

  2. 认识与入门 MarkDown (转Te_Lee)

    Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...

  3. jeesite应用实战(数据增删改查),认真读完后10分钟就能开发一个模块

    jeesite配置指南(官方文档有坑,我把坑填了!)这篇文章里,我主要把jeesite官方给出的帮助文档的坑填了,按照里面的方法可以搭建起来jeesite的站点.系统可以运行以后,就可以进入开发模块了 ...

  4. 正则表达式---01 js篇

    本文主要针对js中正则表达式的实践操作,来让大家对正则表达式有一个入门清晰的了解. 正则表达式推荐学习网址:http://www.runoob.com/regexp/regexp-tutorial.h ...

  5. 利用BandwagonHost***便宜Linux VPS安装VNC(远程桌面)- 安装篇

    如果我们有搭建过网站,或者用过VPS.服务器,肯定会看到最近几年主机市场竞争还是比较激烈的.尤其是VPS.服务器的价格走低,很多商家价格厮杀的厉害,甚至出现年付几美元的VPS.便宜VPS固然是好事,我 ...

  6. jquery UI_tabs

    1.tab使用 <!doctype html> <html lang="en"> <head> <meta charset="u ...

  7. FTP添加虚拟目录(图)

    设置 访问效果 可以看到有这个文件:

  8. HDU2433 最短路 + 剪枝优化

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2433 ,最短路(SPFA或优化过的Dijstra) + 剪枝优化 这道题关键还是在几个剪枝上面,没有剪 ...

  9. 因技术垃圾直接上手groovy的工作感悟

    因为熟悉devops,用IDEA的git完成版本提交,不太喜欢公司的代码控制,从事groovy基本没有代码控制,提交就打个压缩包给指定的人. 没有持续继承,持续部署(CICD). http://xio ...

  10. MySQL8.0在Windows下的安装和使用

    前言 MySQL在Windows下有2种安装方式:1.图形化界面方式安装MySQL 2.noinstall方式安装MySQL.在这里,本文只介绍第二种方式:以noinstall方式安装MySQL,以及 ...