HDU 5492 Find a path(数学 + DP)

探讨了一种从矩阵起点到终点的路径选择问题,旨在找到一条最优路径,通过优化路径上的魔法数值来最大化路径的美丽值。文章深入分析了问题的数学模型,并提供了高效的动态规划算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492


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


题意:(转)

给一个N*M的矩阵,从(1,1)到(N,M)经过的格点分值分别为 Ai ,(路径只能向右或向下走,共N+M-1步)。求各种路径中最大的 (N+M1)N+M1i=1(AiAavg)2 .其中 AavgAi  
N,M分别为1~30的整数, Ai 为不超过30的整数。

PS:转自:http://blog.youkuaiyun.com/baileys0530/article/details/48768123


首先对表达式进行化简: N+M1i=1 先记为  

(N+M1)(AiAavg)2
=(N+M1)(A2i+A2avg2AiAavg)

其中 Aavg=1N+M1Ai ,则 2AiAavg=2(N+M1)A2avg  
=(N+M1)[(A2i)(N+M1)A2avg]
=(N+M1)A2i(Ai)2


N+M-1的是一个定值,那么最后化简得到的式子的值就只和有联系了;

三维:dp[i][j][k]表示从点(1,1)到点(i,j)的值为k时,

的值是dp[i][j][k];

最后再找出min((M+N-1)dp[i][j][k]-k*k)!

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int a[37][37];
int dp[37][37][1810];

int main()
{
    int t;
    int cas = 0;
    int n, m;
    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]);
            }
        }

        memset(dp,INF,sizeof(dp));
        dp[1][0][0]=0;
        dp[0][1][0]=0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                for(int k = 0; k <= 1800; k++)
                {
                    if(dp[i-1][j][k] != INF)
                    {
                        dp[i][j][k+a[i][j]] = min(dp[i][j][k+a[i][j]],dp[i-1][j][k]+a[i][j]*a[i][j]);
                    }
                    if(dp[i][j-1][k] != INF)
                    {
                        dp[i][j][k+a[i][j]] = min(dp[i][j][k+a[i][j]],dp[i][j-1][k]+a[i][j]*a[i][j]);
                    }
                }
            }
        }
        int ans = INF;
        for(int i = 0; i <= 1800; i++)
        {
            if(dp[n][m][i] != INF)
                ans = min(ans,(n+m-1)*dp[n][m][i]-i*i);
        }
        printf("Case #%d: %d\n",++cas,ans);
    }
    return 0;
}
/*
1
2 2
1 2
3 4
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值