码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5569 matrix(简单dp)

时间:2015-11-21 22:35:06      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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)

 


 

 

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

 

 

 

Source

 

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

技术分享
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 1006
23 #define inf 1<<29
24 ll n,m;
25 ll mp[N][N];
26 ll dp[N][N];
27 int main()
28 {
29    while(scanf("%I64d%I64d",&n,&m)==2){
30          memset(dp,0,sizeof(dp));
31       for(ll i=1;i<=n;i++){
32          for(ll j=1;j<=m;j++){
33             scanf("%I64d",&mp[i][j]);
34          }
35       }
36       for(ll i=0;i<=n+1;i++){
37          dp[i][0]=inf;
38          mp[i][0]=inf;
39       }
40       for(ll i=0;i<=m+1;i++){
41          dp[0][i]=inf;
42          mp[0][i]=inf;
43       }
44 
45       dp[1][1]=mp[1][1];
46       dp[1][2]=mp[1][1]*mp[1][2];
47       dp[2][1]=mp[1][1]*mp[2][1];
48       for(ll i=1;i<=n;i++){
49          for(ll j=1;j<=m;j++){
50             if(i==1 && j==1) continue;
51             if(i==1 && j==2) continue;
52             if(i==2 && j==1) continue;
53             if((i+j)%2==0){
54                dp[i][j]=min(dp[i-1][j],dp[i][j-1]);
55                //printf("i , j %d %d %d\n",i,j,dp[i][j]);
56             }
57             else{
58                dp[i][j]=min(dp[i-1][j]+mp[i-1][j]*mp[i][j],dp[i][j-1]+mp[i][j-1]*mp[i][j]);
59                //printf("i , j %d %d %d\n",i,j,dp[i][j]);
60             }
61 
62          }
63       }
64 
65       printf("%I64d\n",dp[n][m]);
66    }
67     return 0;
68 }
View Code

 

hdu 5569 matrix(简单dp)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4984789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!