hdoj5492Find a path【dp】
来源:互联网 发布:开淘宝店要注册公司吗 编辑:程序博客网 时间:2024/06/05 15:22
Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1173 Accepted Submission(s): 511
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) asA1,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.
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
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 integersN 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.
Each test case starts with a line containing two integers
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
12 21 23 4
Sample Output
Case #1: 14
Source
2015 ACM/ICPC Asia Regional Hefei Online
题意:给一个矩形迷宫每个格子上有一个值Ai求从左上角走到右下角使得(N+M−1)
∑N+M−1i=1(Ai−Aavg)2值最小观察此式和方差的计算公式类似因此可利用
方差的计算公式简化此式为(N+M−1)∑A2i−(∑Ai)2
参考博客:http://blog.csdn.net/u012860063/article/details/48790895
/*dp[i][j][k]在第i行第j个格子处所经过的路径和为k时格子的平方和的最小值 */#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#define inf 0x3f3f3f3fusing namespace std;int map[35][35];int dp[35][35][2010];int main(){int t,n,m,i,j,test=1,k;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=1;i<=n;++i){for(j=1;j<=m;++j){scanf("%d",&map[i][j]);}}memset(dp,0x3f,sizeof(dp));dp[1][0][0]=dp[0][1][0]=0;for(i=1;i<=n;++i){for(j=1;j<=m;++j){for(k=0;k<=1800;++k){dp[i][j][k+map[i][j]]=min(dp[i][j][k+map[i][j]],min(dp[i-1][j][k],dp[i][j-1][k])+map[i][j]*map[i][j]);}}}int ans=inf;for(k=0;k<=1800;++k){if(dp[n][m][k]!=inf)ans=min(ans,dp[n][m][k]*(n+m-1)-k*k);}printf("Case #%d: %d\n",test++,ans);}return 0;}
i=1∑N+M−1i=1)
0 0
- hdoj5492Find a path【dp】
- hdu5492Find a path+DP
- hdu5492Find a path dp
- hdu5492 Find a path dp
- hdu5492 Find a path(DP)
- HDU5492 Find a path DP
- HDU5492 Find a path【DP】
- hdu 5492 Find a path (DP)
- [dp] hdu 5492 Find a path
- HDU5492 Find a path (DP+水题)
- hdu 5492 Find a path DP
- hdu 5492 Find a path【dp】
- DP HDOJ 5492 Find a path
- hdu 5492 Find a path(dp)
- HDU 5492 Find a path(DP)
- HDU 5492 Find a path(DP)
- HDU 5492 Find a path【DP】
- 状压DP HDU3538 A sample Hamilton path
- postgresql 删除ID重复数据
- [从头学数学] 第43节 多位数乘一位数
- 给定点,绘制路径,点击point,弹出infoTemplate
- thinkphp U 方法加密传参数
- QRadioButton实现详解
- hdoj5492Find a path【dp】
- C# 冒泡排序
- linux常用基本命令之五wget
- strong和weak的区别
- 在IT行业,科技公司,技术重要还是市场销售重要!
- 修改Android签名证书keystore的密码、别名alias以及别名密码
- Automake的标准工程组织
- Source和Sink:可以把source理解为发送端的流,sink理解为接受端。MediaSink是各种类型的Sink的基类,MediaSource是各种类型Source的基类,各种类型的流媒体格式
- H3C交换机常用命令