matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s):
35 Accepted Submission(s): 26
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 isa1∗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
题意:给定n*m矩阵(保证n+m为奇数),每个位置(i, j)有一个价值a[i][j]。现在要从(1, 1) 到达 (n, m),每次只能向下或者向右走,依次记录途中经过位置a[1] ... a[2*k+1]。要求最后的a[1]*a[2] + ... + a[2*k]*a[2*k+1]值最小,问最小的值。
很裸的dp吧,用dp[i][j]记录到达位置(i, j)时胡最小值。然后暴力转移就好了。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define MAXN 100
#define MAXM 1010
#define eps 1e-8
#define LL long long
using namespace std;
LL a[1100][1100];
LL dp[1100][1100];
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%lld", &a[i][j]);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(i == 1 && j == 1)
dp[i][j] = 0;
else if((i+j) & 1)
{
if(i == 1)
dp[i][j] = dp[i][j-1] + a[i][j] * a[i][j-1];
else if(j == 1)
dp[i][j] = dp[i-1][j] + a[i][j] * a[i-1][j];
else
dp[i][j] = min(dp[i-1][j]+a[i][j]*a[i-1][j], dp[i][j-1]+a[i][j]*a[i][j-1]);
}
else
{
if(i == 1)
dp[i][j] = dp[i][j-1];
else if(j == 1)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = min(dp[i-1][j], dp[i][j-1]);
}
}
}
printf("%lld\n", dp[n][m]);
}
return 0;
}