HDU 5492 Find a path(DP)——2015 ACM/ICPC Asia Regional Hefei Online

Find a path

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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
 

Source
2015 ACM/ICPC Asia Regional Hefei Online
 
/*********************************************************************/

题意:给你一个N*M的迷宫,迷宫内每个点都有个权值,现要从(1,1)移动到(N,M),每次只能往下或往右走,即在点(x,y)时,只能移动到(x+1,y)或是(x,y+1)的位置,问怎么走使得经过的N+M-1个点的权值A1,A2,…,An+m-1满足(N+M1)N+M1i=1(AiAavg)2值最小

首先,因为公式涉及权值的平均数,不妨将其化简一下

HDU 5492 Find a path(DP)——2015 ACM/ICPC Asia Regional Hefei Online_第1张图片

N+M-1的值是固定的,所以只跟有关

又因为Ai的值是不超过30的,而N+M-1个Ai相加最多不超过1800,故我们可以用个三维dp来解此题,dp[i][j][k]表示从点(1,1)到点(i,j)的值为k时,的值是dp[i][j][k],那么我们最后只需求出min((M+N-1)dp[i][j][k]-k*k)即可

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 35;
const int inf = 100000000;
const int mod = 2009;
int s[N][N],dp[N][N][1805];
int main()
{
    int t,i,j,k,n,m,p=1,Min,q;
    scanf("%d",&t);
    while(t--)
    {
        Min=inf;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&s[i][j]);
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                for(k=0;k<=1800;k++)
                {
                    dp[i][j][k]=inf;
                    if(i==1&&j==1&&k==s[i][j])
                        dp[i][j][k]=s[i][j]*s[i][j];
                    if(k>=s[i][j])
                    {
                        if(i>1)
                            dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k-s[i][j]]+s[i][j]*s[i][j]);
                        if(j>1)
                            dp[i][j][k]=min(dp[i][j][k],dp[i][j-1][k-s[i][j]]+s[i][j]*s[i][j]);
                    }
                }
        for(q=n+m-1,k=0;k<=1800;k++)
            if(dp[n][m][k]!=inf)
                Min=min(Min,q*dp[n][m][k]-k*k);
        printf("Case #%d: %d\n",p++,Min);
    }
    return 0;
}
菜鸟成长记

你可能感兴趣的:(动态规划,ACM)