Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 121 Accepted Submission(s): 83
2 3 1 2 3 2 2 1 2 3 2 2 1 1 2 4
4 8
#include<cstdio> #include<cstring> #define INF 0x3f3f3f int a[1010][1010]; int dp[1010][1010]; int min(int a,int b) { return a>b?b:a; } int main() { int n,m,i,j; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;++i) for(j=0;j<m;++j) { scanf("%d",&a[i][j]); dp[i][j]=INF; } if(n>0) dp[1][0]=a[0][0]*a[1][0]; if(m>0) dp[0][1]=a[0][0]*a[0][1]; for(i=0;i<n;++i) { for(j=0;j<m;j++) { if((i+j)&1) { if(i-1>=0&&j-1>=0) dp[i][j]=dp[i-1][j-1]+a[i][j]*min(a[i-1][j],a[i][j-1]); if(i-2>=0) dp[i][j]=min(dp[i][j],dp[i-2][j]+a[i][j]*a[i-1][j]); if(j-2>=0) dp[i][j]=min(dp[i][j],dp[i][j-2]+a[i][j]*a[i][j-1]); } } } printf("%d\n",dp[n-1][m-1]); } return 0; }