zoukankan      html  css  js  c++  java
  • hdu-5569 matrix(dp)

    题目链接:

    matrix

    Time Limit: 6000/3000 MS (Java/Others)  

      Memory Limit: 65536/65536 K (Java/Others)


    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 3
    1 2 3
    2 2 1
    2 3
    2 2 1
    1 2 4
     
    Sample Output
    4
    8
     
    题意:
     
    问怎么走才能使这个式子的值尽量小;
     
    思路:
     
    dp水题;
     
    AC代码;
     
    /************************************************ 
    ┆  ┏┓   ┏┓ ┆    
    ┆┏┛┻━━━┛┻┓ ┆ 
    ┆┃       ┃ ┆ 
    ┆┃   ━   ┃ ┆ 
    ┆┃ ┳┛ ┗┳ ┃ ┆ 
    ┆┃       ┃ ┆  
    ┆┃   ┻   ┃ ┆ 
    ┆┗━┓    ┏━┛ ┆ 
    ┆  ┃    ┃  ┆       
    ┆  ┃    ┗━━━┓ ┆ 
    ┆  ┃  AC代马   ┣┓┆ 
    ┆  ┃           ┏┛┆ 
    ┆  ┗┓┓┏━┳┓┏┛ ┆ 
    ┆   ┃┫┫ ┃┫┫ ┆ 
    ┆   ┗┻┛ ┗┻┛ ┆       
    ************************************************ */  
    
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    
    using namespace std;
    
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    
    typedef  long long LL;
    
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=1e6+10;
    const int maxn=1e3+4;
    const double eps=1e-8;
    
    int a[maxn][maxn],dp[maxn][maxn];
    
    int main()
    {       
            int n,m;
            while(cin>>n>>m)
            {
                For(i,1,n)For(j,1,m)read(a[i][j]);
                For(i,0,n)For(j,0,m)dp[i][j]=inf;
                dp[0][1]=dp[1][0]=0;
                For(i,1,n)
                {
                    For(j,1,m)
                    {
                        if((i+j)&1)dp[i][j]=min(dp[i-1][j]+a[i-1][j]*a[i][j],dp[i][j-1]+a[i][j-1]*a[i][j]);
                        else dp[i][j]=min(dp[i-1][j],dp[i][j-1]);
                    }
                }
                cout<<dp[n][m]<<"
    ";
            }
            return 0;
    }
    

      

  • 相关阅读:
    12 购物车之一(用列表)
    centos7安装pycharm,并创建桌面快捷方式
    centos7安装xrdp
    centos7 安装gitlab
    mysql 新建用户和授权
    django项目连接mysql没有Mysqldb解决办法_使用pymysql代替
    selenium+python实现查询和下载文件
    Python3-使用Requests和正则表达式爬取猫眼Top100的数据
    python3-字典常用操作
    python3-列表常用操作
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5728266.html
Copyright © 2011-2022 走看看