HDU5492 十分数学的DP

本文介绍了一个关于迷宫寻路的问题,通过动态规划的方法找到从起点到终点路径中具有最小美的路径。路径的美由路径上各点的魔法值决定,并通过方差来衡量路径的整体稳定性。

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

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+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.

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 integers N 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.

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

这题我一开始就是不知道到底怎么个转移法。。
真的不知道
我甚至想出一种转移路径的方式
想过两遍dp
但是时间爆炸…

最后突然发现他的结果用的不是浮点型。。
这就很因垂死听了…
有平均数怎么可能没有浮点呢…
全拆了之后发现了乱七八糟的分母抵消掉了
就留了一坨整数、、
这才做了出来

#include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
int dp[31][31][1900];
int tu[31][31];
int n, m;
int inf = 1900*31*31;
int main()
{
    int T;
    cin >> T;
    int u = 0;
    while (T--)
    {
        cin >> n >> m;
        memset(tu, 0, sizeof(tu));
        for (int a = 1;a <= n;a++)
        {
            for (int b = 1;b <= m;b++)
            {
                cin >> tu[a][b];
                for (int c = 0;c <= 1899;c++)
                {
                    dp[a][b][c] = inf;
                }
            }
        }
        dp[1][1][tu[1][1]] = tu[1][1] * tu[1][1];
        for (int a = 1;a <= n;a++)
        {
            if (a < n)
            {
                for (int b = 1;b <= m;b++)
                {
                    for (int c = 0;c <= 1800;c++)
                    {
                        if (b < m)
                        {
                            if(dp[a][b][c]!=inf)dp[a][b+1][c + tu[a][b+1]] = min(dp[a][b][c] + tu[a][b+1] * tu[a][b+1], dp[a][b+1][c + tu[a][b+1]]);
                        }
                        if (dp[a][b][c] != inf)dp[a + 1][b][c + tu[a + 1][b]] = min(dp[a][b][c] + tu[a + 1][b] * tu[a + 1][b],dp[a+1][b][c + tu[a + 1][b]]);
                    }
                }
            }
            else
            {
                for (int b = 1;b <= m;b++)
                {
                    for (int c = 0;c <= 1800;c++)
                    {
                        if (b < m)
                        {
                            if (dp[a][b][c] != inf)dp[a][b + 1][c + tu[a][b + 1]] = min(dp[a][b][c] + tu[a][b + 1] * tu[a][b + 1], dp[a][b + 1][c + tu[a][b + 1]]);
                        }
                    }
                }
            }
        }
        int sum = inf;
        for (int a = 0;a <= 1770;a++)
        {
            if(dp[n][m][a]!= inf)
            sum = min(sum, (n+m-1)*dp[n][m][a]-a*a);
        }
        printf("Case #%d: %d\n", ++u, sum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值