HDU 5569 matrix(DP)——BestCoder Round #63(div.1 div.2)

76 篇文章 0 订阅
22 篇文章 0 订阅

matrix

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


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 steps. Let the numbers you go through become an array  a1,a2,...,a2k .     The cost is  a1a2+a3a4+...+a2k1a2k . What is the minimum of the cost?
 

Input
Several test cases(about  5 )

For each cases, first come 2 integers,  n,m(1n1000,1m1000)

N+m is an odd number.

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

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
 

Source
 

/************************************************************************/

附上该题对应的中文题

matrix

 
 
 Time Limit: 6000/3000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
给定n*mnm(n+mn+m为奇数)的矩阵,从(1,1)(1,1)走到(n,m)(n,m)且只能往右往下走,设经过的数为a_1, a_2 ... a_{2k}a1,a2...a2k,贡献为a_1*a_2+a_3*a_4+...+a_{2k-1}*a_{2k}a1a2+a3a4+...+a2k1a2k,求最小贡献。
输入描述
若干组数据(大概55组)。
每组数据第一行两个数n,m(1 \leq n,m \leq 1000n,m(1n,m1000n+mn+m为奇数)。
接下来nn行每行mm个数a_i, j(1 \leq ai,j \leq 100)ai,j(1ai,j100)描述这个矩阵。
输出描述
对于每组数据,输出一行表示答案。
输入样例
2 3
1 2 3
2 2 1
2 3
2 2 1
1 2 4
输出样例
4
8
/****************************************************/

出题人的解题思路:

matrix

dp[i][j]dp[i][j]表示当前走到第i,ji,j个位置的最小贡献,我们可以假定(i+j)(i+j)为奇数,由该状态可以转移向最多44个位置,就可以了。

话说,这次的出题人是不是比较喜欢dp,连着出了两题dp题,让人有点小醉。这题很明显的dp题,然而,我逆着来就跪了好久好久,先是边界控制不好,再是条件控制不好,总之做得很糟糕,就连题目也是比赛结束之后一发A的,着实被学弟学妹们虐了一把
这题绝大多数人应该都是正向推导的,可能我比较奇葩,逆着来,对于(i+j)为偶数的位置,判断右边和下边与当前位置的乘积再加上先前求得的值来判断取哪边
而对于(i+j)为奇数的位置,则是哪边结果小取哪边
放上两组数据参考参考:
INPUT
3 4
1 2 3 4
4 3 2 1
1 2 4 3
OUTPUT
11

INPUT
4 3
4 2 3
2 4 3
3 4 2
2 3 4
OUTPUT
25
该好好学dp去了,欢迎大家前来指点
#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<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-4
using namespace std;
const int N = 1005;
const int M = 15005;
const int inf = 100000;
const int mod = 2009;
int s[N][N],w[N][N];
int main()
{
    int n,m,i,j;
    while(~scanf("%d%d",&n,&m))
    {
        memset(w,0,sizeof(w));
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&s[i][j]);
        for(i=n;i>0;i--)
            for(j=m;j>0;j--)
            {
                if(i==n&&j==m)
                {
                    w[i][j]=0;
                    continue;
                }
                if((i+j)%2==0)
                {
                    if(i+1>n)
                    {
                        s[i][j]*=s[i][j+1];
                        w[i][j]=s[i][j]+w[i][j+1];
                        continue;
                    }
                    else if(j+1>m)
                    {
                        s[i][j]*=s[i+1][j];
                        w[i][j]=s[i][j]+w[i+1][j];
                        continue;
                    }
                    if(s[i][j]*s[i][j+1]+w[i][j+1]<s[i][j]*s[i+1][j]+w[i+1][j])
                    {
                        s[i][j]*=s[i][j+1];
                        w[i][j]=s[i][j]+w[i][j+1];
                    }
                    else
                    {
                        s[i][j]*=s[i+1][j];
                        w[i][j]=s[i][j]+w[i+1][j];
                    }
                }
                else
                {
                    if(i<n&&j<m)
                        w[i][j]=min(w[i][j+1],w[i+1][j]);
                    else if(j+1>m)
                        w[i][j]=w[i+1][j];
                    else
                        w[i][j]=w[i][j+1];
                }
            }
        printf("%d\n",w[1][1]);
    }
    return 0;
}
菜鸟成长记
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值