hdu 5569 matrix(简单dp)

时间:2023-11-29 14:17:56
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 (,) 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 isa1∗a2+a3∗a4+...+a2k−∗a2k. What is the minimum of the cost?
 
Input
Several test cases(about )

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

N+m is an odd number.

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

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

Sample Output

Source

令dp[i][j]表示当前走到第i,j个位置的最小贡献,初始化做好了,然后根据i+j是奇数偶数的情况分别计算dp即可,最后要用long long。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1<<29
ll n,m;
ll mp[N][N];
ll dp[N][N];
int main()
{
while(scanf("%I64d%I64d",&n,&m)==){
memset(dp,,sizeof(dp));
for(ll i=;i<=n;i++){
for(ll j=;j<=m;j++){
scanf("%I64d",&mp[i][j]);
}
}
for(ll i=;i<=n+;i++){
dp[i][]=inf;
mp[i][]=inf;
}
for(ll i=;i<=m+;i++){
dp[][i]=inf;
mp[][i]=inf;
} dp[][]=mp[][];
dp[][]=mp[][]*mp[][];
dp[][]=mp[][]*mp[][];
for(ll i=;i<=n;i++){
for(ll j=;j<=m;j++){
if(i== && j==) continue;
if(i== && j==) continue;
if(i== && j==) continue;
if((i+j)%==){
dp[i][j]=min(dp[i-][j],dp[i][j-]);
//printf("i , j %d %d %d\n",i,j,dp[i][j]);
}
else{
dp[i][j]=min(dp[i-][j]+mp[i-][j]*mp[i][j],dp[i][j-]+mp[i][j-]*mp[i][j]);
//printf("i , j %d %d %d\n",i,j,dp[i][j]);
} }
} printf("%I64d\n",dp[n][m]);
}
return ;
}