hdoj 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): 795    Accepted Submission(s): 344


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
 



题意:有一个N*M的矩阵,矩阵的每个位置都有对应的权值。现在要从左上角走到右下角,且每次只能由(i, j) 到达(i+1, j) 或者 (i, j+1)。对于任意一种可行方案,沿着路线我们可以得到一个A[1], A[2], A[3],...A[N+M-1]的序列,序列元素为经过某一位置的权值。问你最小的 (N+M1)N+M1i=1(AiAavg)2



首先将上面式子化简—— (N+M-1) *( (A[1] + A[2] + ... + A[N+M-1]) - A[avg] ^ 2) = (N+M-1) * (A[1] + ... + A[N+M-1]) - (A[1] + ... + A[N+M+1]).

得到——原式 = (N+M-1) * (权值平方和) - (权值和平方),然后用dp维护最小值就ok了。



用dp[i][j][k] 表示 经过位置(i, j)且权值和为k时的  最小的权值平方和。显然答案就是(N+M-1) * dp[N][M][k] - k*k 其中k为所有可能取得的权值和。


状态转移方程:由(i, j)推导(i+1, j) 和 (i, j+1),这里用Map[i][j]表示位置(i, j)的权值。

(i, j) -> (i+1, j) —— dp[i+1][j][k+Map[i+1][j]] = min(dp[i+1][j][k+Map[i+1][j]], dp[i][j][k] + Map[i+1][j] * Map[i+1][j]);

(i, j) -> (i, j+1) —— dp[i][j+1][k+Map[i][j+1]] = min(dp[i][j+1][k+Map[i][j+1]], dp[i][j][k] + Map[i][j+1] * Map[i][j+1]);


最后找到最小的dp[N][M][k]就可以了。



AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int dp[31][31][2000];
int Map[31][31];
int main()
{
    int t, p = 1;
    scanf("%d", &t);
    int n, m;
    while(t--)
    {
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
                scanf("%d", &Map[i][j]);
        }
        int have = (n+m-1)*30;
        memset(dp, INF, sizeof(dp));
        dp[1][1][Map[1][1]] = Map[1][1]*Map[1][1];
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                for(int k = 0; k <= have; k++)
                {
                    if(dp[i][j][k] != INF)
                    {
                        if(i != n)
                            dp[i+1][j][k+Map[i+1][j]] = min(dp[i+1][j][k+Map[i+1][j]], dp[i][j][k] + Map[i+1][j]*Map[i+1][j]);
                        if(j != m)
                            dp[i][j+1][k+Map[i][j+1]] = min(dp[i][j+1][k+Map[i][j+1]], dp[i][j][k] + Map[i][j+1]*Map[i][j+1]);
                    }
                }
            }
        }
        int ans = INF;
        for(int i = Map[n][m]; i <= have; i++)
            if(dp[n][m][i] != INF)
                ans = min(ans, (n+m-1) * dp[n][m][i] - i*i);
        printf("Case #%d: %d\n", p++, ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值