首页 > 代码库 > hdu-5492 Find a path(dp)

hdu-5492 Find a path(dp)

题目链接:

Find a path

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


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+M1, and Aavg is the average value of all Ai. The beauty of the path is (N+M1) multiplies the variance of the values:(N+M1)N+M1i=1(AiAavg)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 (T50).
Each test case starts with a line containing two integers N and M (1N,M30). 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
 
题意:
 
找一条从(1,1)到(n,m)的路径使得上面那个式子的值最小;
 
思路:
 
最后那个式子可以化简成(n+m-1)*∑Ai2-(∑Ai)2;
dp[i][j][k]表示到达(i,j)时和为k的平方和;转移就好转移了,结果也可以表示出来了;
有时候不能直接用dp某某表达,所以需要转换一下思路;
 
AC代码:
 
#include <bits/stdc++.h>using namespace std;  const int inf=1e7;const int maxn=1e5+10;int n,m,a[40][40];int dp[32][32][61*31];int QAQ(int x){return x*x;}int main()  {      int t,Case=0;    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",&a[i][j]);            int hi=(n+m)*31;            for(int i=1;i<=n;i++)                for(int j=1;j<=m;j++)                    for(int k=0;k<=hi;k++)                        dp[i][j][k]=inf;            dp[1][1][a[1][1]]=QAQ(a[1][1]);            for(int i=1;i<=n;i++)            {                for(int j=1;j<=m;j++)                {                    for(int k=0;k<=hi;k++)                    {                        dp[i][j+1][k+a[i][j+1]]=min(dp[i][j+1][k+a[i][j+1]],dp[i][j][k]+QAQ(a[i][j+1]));                        dp[i+1][j][k+a[i+1][j]]=min(dp[i+1][j][k+a[i+1][j]],dp[i][j][k]+QAQ(a[i+1][j]));                    }                }            }        int ans=inf;        for(int i=0;i<=hi;i++)ans=min(ans,(n+m-1)*dp[n][m][i]-QAQ(i));        printf("Case #%d: %d\n",++Case,ans);    }    return 0;  }  

  

  

hdu-5492 Find a path(dp)