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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int mp[][];
int dp[][];
const int inf = 1e9+;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
dp[i][j]=inf;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
mp[i][j]=read();
dp[][]=,dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if((i+j)%)
{
dp[i][j]=inf;
if(i>&&j>)
dp[i][j]=min(dp[i][j],dp[i-][j-]+min(mp[i-][j]*mp[i][j],mp[i][j-]*mp[i][j]));
if(i>)
dp[i][j]=min(dp[i][j],dp[i-][j]+mp[i-][j]*mp[i][j]);
if(j>)
dp[i][j]=min(dp[i][j],dp[i][j-]+mp[i][j]*mp[i][j-]);
}
}
}
printf("%d\n",dp[n][m]);
}
}
04-21 09:02