hdu 5569 matrix dp

matrix

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5569

Description

Given a matrix with n rows and m columns ( n+m is an odd number ), at first , you begin with the number at top-left corner (1,1) and you want to go to the number at bottom-right corner (n,m). And you must go right or go down every steps. Let the numbers you go through become an array a1,a2,...,a2k. The cost is a1∗a2+a3∗a4+...+a2k−1∗a2k. What is the minimum of the cost?

Input

Several test cases(about 5)

For each cases, first come 2 integers, n,m(1≤n≤1000,1≤m≤1000)

N+m is an odd number.

Then follows n lines with m numbers ai,j(1≤ai≤100)

Output

For each cases, please output an integer in a line as the answer.

Sample Input

2 3
1 2 3
2 2 1
2 3
2 2 1
1 2 4

Sample Output

4
8

HINT

题意

给定n*m(n+m为奇数)的矩阵,从(1,1)走到(n,m)且只能往右往下走,设经过的数为a1,a2,..,a2k,贡献为a1*a2+a3*a4...+a2k-1*a2k,求最小贡献

题解:

dp[i][j]表示走到i,j的最小贡献,我们只考虑(i+j)为奇数的时候就好了

然后转移的时候,也只会从奇数位置转移过来

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m;
int mp[1005][1005];
int dp[1005][1005];
const int inf = 1e9+5;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<=n;i++)
            for(int j=0;j<=m;j++)
                dp[i][j]=inf;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                mp[i][j]=read();
        dp[1][0]=0,dp[0][1]=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if((i+j)%2)
                {
                    dp[i][j]=inf;
                    if(i>1&&j>1)
                        dp[i][j]=min(dp[i][j],dp[i-1][j-1]+min(mp[i-1][j]*mp[i][j],mp[i][j-1]*mp[i][j]));
                    if(i>1)
                        dp[i][j]=min(dp[i][j],dp[i-2][j]+mp[i-1][j]*mp[i][j]);
                    if(j>1)
                        dp[i][j]=min(dp[i][j],dp[i][j-2]+mp[i][j]*mp[i][j-1]);
                }
            }
        }
        printf("%d\n",dp[n][m]);
    }
}
时间: 2025-01-09 07:12:12

hdu 5569 matrix dp的相关文章

hdu 5569 matrix(简单dp)

Problem Description Given a matrix with n rows and m columns ( n+m is an odd number ), at first , you begin with the number at top-left corner (1,1) and you want to go to the number at bottom-right corner (n,m). And you must go right or go down every

(动态规划)matrix -- hdu -- 5569

http://acm.hdu.edu.cn/showproblem.php?pid=5569 matrix Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 325    Accepted Submission(s): 196 Problem Description Given a matrix with n rows and m colu

HDU 2830 Matrix Swapping II (DP,最大全1矩阵)

题意  给你一个n*m矩阵  每列都可以随便交换位置   求最优交换后最大的全1子矩阵 又是HDU 1505 1506的变种  但这个更容易了  因为每列都可以交换位置了   那么这一行中所有比i高的都可以与i相邻了  只需要统计这一行有多少个比i高就行了   可以在算出每一行后  把高度大的放前面去  用num[i]记录排序后的列原来的数  这样就有j列比h[i][num[j]]高了  最后的答案也就是max(j*h[i][num[j]]) #include<cstdio> #include

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

HDU 2845 Beans(DP,最大不连续和)

题意    吃豆子游戏    当你吃了一个格子的豆子   该格子左右两个和上下两行就不能吃了    输入每个格子的豆子数    求你最多能吃多少颗豆子 可以先求出每行你最多可以吃多少颗豆子   然后每行就压缩成只有一个格子了   里面的豆子数就是那一行最多可以吃的豆子数   然后问题就变成求一列最多可以吃多少颗豆子了   和处理每一行一样处理   那么问题就简化成求一行数字的最大不连续和问题了 令d[i]表示某一行前i个豆子的最大和  有两种情况  吃第i个格子中的豆子和不吃第i个格子中的豆子

hdu 2686 Matrix &amp;&amp; hdu 3367 Matrix Again (最大费用最大流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1394    Accepted Submission(s): 758 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number

hdu 2666 Matrix

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1332    Accepted Submission(s): 740 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>