matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 450 Accepted Submission(s): 270
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
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)
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
#include <bits/stdc++.h>
using namespace std;
#define inf (1<<30)
__int64 a[1005][1005];
int dp[1005][1005];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
for(int k=1;k<=m;k++)
scanf("%I64d",&a[i][k]);
dp[0][0]=dp[1][0]=dp[0][1]=0;
a[0][0]=a[1][0]=a[0][1]=inf;
for(int i=2;i<=n;i++)dp[i][0]=a[i][0]=inf;
for(int k=2;k<=m;k++)dp[0][k]=a[0][k]=inf;
for(int i=1;i<=n;i++)
for(int k=1;k<=m;k++)
if((i+k)%2==0)
dp[i][k]=min(dp[i-1][k],dp[i][k-1]);
else
dp[i][k]=min(dp[i-1][k]+a[i-1][k]*a[i][k],dp[i][k-1]+a[i][k-1]*a[i][k]);
printf("%d\n",dp[n][m]);
}
return 0;
}