hdu 5492 Find a path(公式推导,DP)
来源:互联网 发布:美国反恐战争数据图 编辑:程序博客网 时间:2024/05/20 08:21
Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1409 Accepted Submission(s): 607
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
题意:求从(1,1)到(n,m)的一条路径,使得(N+M−1)∑N+M−1i=1(Ai−Aavg)2最小
思路:
把公式一步步化简后可以得到结果等于(n+m-1)*(A1²+A2²+...+A(n+m-1)²)-(A1+A2+...+An+m-1)² )
由于数据量很小,只有30*30,并且有一条路径最多权值为60*30 所以我们可以用DP去做
dp[i][j][k]表示在(i,j)这个点的时候路径之和为k的时候A1²+A2²+...+Ak²的最小值
最后去枚举k,求出最小的即可
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 31#define INF 99999999int ma[N][N];int dp[N][N][N*60];int main(){ int T,n,m,tot=1; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&ma[i][j]); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) for(int k=0;k<=(n+m-1)*30;k++) dp[i][j][k]=INF; dp[1][1][ma[1][1]]=ma[1][1]*ma[1][1]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) for(int k=0;k<=(n+m-1)*30;k++) { if(i>1&&k>=ma[i][j]&&dp[i-1][j][k-ma[i][j]]!=INF) dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k-ma[i][j]]+ma[i][j]*ma[i][j]); if(j>1&&k>=ma[i][j]&&dp[i][j-1][k-ma[i][j]]!=INF) dp[i][j][k]=min(dp[i][j][k],dp[i][j-1][k-ma[i][j]]+ma[i][j]*ma[i][j]); } int ans=INF; for(int i=1;i<=(n+m-1)*30;i++) { if(dp[n][m][i]==INF) continue; ans=min(ans,(n+m-1)*dp[n][m][i]-i*i); } printf("Case #%d: %d\n",tot++,ans); } return 0;}
0 0
- hdu 5492 Find a path(公式推导,DP)
- 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] hdu 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】
- HDU 5492 Find a path(数学 + DP)
- HDU 5492 Find a path
- HDU 5492 Find a path
- hdu 5492 Find a path
- HDU 5492 Find a path
- HDU 5492 Find a path (dp) 2015合肥网络赛
- DP HDOJ 5492 Find a path
- Find a path HDU
- zzuli 1907: 小火山的宝藏收益(邻接表加dfs)
- openGL 纹理使用
- 【基础-java-BIO流】BIO(1)常用IO流引索
- idea 安装 activate-power-mode
- poj2441 状压dp
- hdu 5492 Find a path(公式推导,DP)
- zzuliOJ 1913:小火山的计算能力(栈+水题)
- UVALive 7324ASCII Addition
- 模拟鼠标按下
- Scroller的作用
- win7 64位下python2.7安装scipy科学计算库
- OC学习日记09-字典
- Codeforces 589B Layer Cake(两次排序)
- Java线程问题实现打印数字字母