• hdu 5569 matrix(简单dp)


    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
    ",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
    ",i,j,dp[i][j]);
    60             }
    61 
    62          }
    63       }
    64 
    65       printf("%I64d
    ",dp[n][m]);
    66    }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    Python开发:关于__name__
    学习笔记之cocos2dx2.1.1实现修改plist文件数据,用TinyXml解析XML
    计算机相关书籍推荐(持续更新)
    【C语言】溢出的处理及大小端模式的判断
    使用 MDSD 开发安全可靠的软件
    关于 /dev/null 与 /dev/zero
    NetBeans 时事通讯(刊号 # 108 Jul 08, 2010)
    NetBeans 时事通讯(刊号 # 109 Jul 17, 2010)
    JDK1.6 Update21 下载
    类的内联函数的实现应该放在哪里
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4984789.html
Copyright © 2020-2023  润新知