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. centos6安装bochs

    安装包 bochs 2.6.8 平台 centos6 前提依赖 yum groupinstall -y "Server Platform Development" "De ...

  2. Linux软件相关记录

    Pidgin+lw-web的聊天记录的文件对应的目录为.purple/logs/webqq/你的QQ号码/,进入之后有选择的删除. mkdir -p 递归创建目录:pwd 显示当前目录:cd .. 回 ...

  3. vue awaresome swiper的使用

    main.jsimport VueAwesomeSwiper from 'vue-awesome-swiper'import 'swiper/dist/css/swiper.css'Vue.use(V ...

  4. sonarque下载和安装使用

    参考地址: https://www.cnblogs.com/qiumingcheng/p/7253917.html https://www.cnblogs.com/cjsblog/p/10735800 ...

  5. 《Unity預計算即時GI》笔记:三、Clusters和总结

    Clusters 叢集,透過修改叢集(Clusters)也是一個降低Unity預計算流程所需要執行的工作數量的好方法.降低叢集數量也能提高執行時的效能. 當採用PRGI來計算場景光照時,Unity會簡 ...

  6. nginx学习书籍推荐

    最好的书是源码 深入理解NGINX" 陶辉著 <实战Nginx...>张宴 <深入理解Nginx:模块开发与架构解析> nginx开发从入门到精通 Nginx HTT ...

  7. (转载)office 2003 gaozhi.msi 缺失提示问题修复

    某些GHOST版win7,自带office 2003,每次启动word,它都会提示"稿纸没安装"云云,找不到那个文件.可是我搜遍了硬盘,确实没有那个文件.每次都要点取消,这个提示才 ...

  8. POJ2112 Optimal Milking---二分+Floyd+网络流

    题目链接: https://vjudge.net/problem/POJ-2112 题目大意: k个机器,每个机器最多服务m头牛. c头牛,每个牛需要1台机器来服务. 告诉你牛与机器每个之间的直接距离 ...

  9. 【BZOJ1057】[ZJOI2007] 棋盘制作(单调栈的运用)

    点此看题面 大致题意: 给你一个\(N*M\)的\(01\)矩阵,要求你分别求出最大的\(01\)相间的正方形和矩形(矩形也可以是正方形),并输出其面积. 题解 这题第一眼看去没什么思路,仔细想想,能 ...

  10. matlplotlib 为折线图填充渐变颜色

    概要   本篇记录绘图时填充颜色时的一些常用设置,主要用到了 imshow,fill 函数.   填充图实例   填充的效果图如下: 图 1:渐变色效果图 可根据下方给出的代码进行自定义. #!/us ...