hdu 5569(dp)

来源:互联网 发布:沪昆高铁偷工减料知乎 编辑:程序博客网 时间:2024/06/05 19:26

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 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 31 2 32 2 12 32 2 11 2 4
 

Sample Output
48
 
#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;}


0 0
原创粉丝点击