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. koa2 从入门到进阶之路 (四)

    之前的文章我们介绍了一下 koa 中间件 以及 koa 中间件的洋葱图执行流程,本篇文章我们来看一下 koa 中使用 ejs 模板及页面渲染. 在 Express 中,我们经常会用 ejs 模板来渲染 ...

  2. mfc 列表控件

    经常使用的mfc控件:list control,记录下   首先将list control拖入到对话框中,然后命名ID,通过类向导,类型为control,控件变量名(m_showlist),  vie ...

  3. CSS里的 no-repeat

    简单来说,CSS里的 no-repeat是针对背景图片来说的.当你设置了no-repeat这个属性后,你的背景图片将不会被重复,再换一种说法,你在网站上所看到的背景图片就是你所添加的图片, 不会出现平 ...

  4. Oracle使用jdbc调用带游标参数的存储过程

    package com.jckb.procedure; import java.sql.CallableStatement; import java.sql.Connection; import ja ...

  5. Photoshop之切图

    基本(繁琐)操作: 切JPG图(即带背景的图): 1.         选切片工具(另外,切片选择工具能选择切片和删除切片),切 2.         存储为Web所用格式(快捷键Ctrl + Shi ...

  6. html的语法 3

    <html> <head> <title>这是第一节课网页标题</title> <!--meta charset="UTF-8" ...

  7. ArcGIS API for Javascript 使用缓冲区结果做query查询出现“esri.config.defaults.io.proxyUrl 尚未进行设置”错误

    1.前言 在研究ArcGIS API for JavaScript时会遇到这样的问题,比如我们在做缓冲区分析时,用分析的范围作为空间查询query的参数,在执行结果中总是会看到“esri.config ...

  8. IntelliJ IDEA IDEA 2018 激活注册码

    K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...

  9. 重置SQLSERVER表的自增列,让自增列重新计数【转】

    很多时候我们需要重置某个表的自增列,让自增列重新从1开始记数.最蠢的方法当然是把该表删掉再重新建表了.其实,还有其它的方法可以重置自增列的值: 方法一:使用TRUNCATE TABLE语句: TRUN ...

  10. vue-初识

    一:vue基础1.1.Vue是一套构建用户界面的渐进式框架1.2.引入vue:<script src="https://unpkg.com/vue/dist/vue.js"& ...