(动态规划)matrix -- hdu -- 5569

http://acm.hdu.edu.cn/showproblem.php?pid=5569

matrix

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 325    Accepted Submission(s): 196

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 a1∗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

BestCoder Round #63 (div.2)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;

const int N = 1100;

int a[N][N], dp[N][N];

int main()
{
    int n, m, i, j;

    while(scanf("%d%d", &n, &m)!=EOF)
    {
        for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
            scanf("%d", &a[i][j]);

        memset(dp, 0x3f3f3f3f, sizeof(dp));

        for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
        {
            if(i==1 && j==1)
                dp[i][j] = 0;
            else 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]);
        }

        printf("%d\n", dp[n][m]);
    }
    return 0;
}
时间: 2024-07-30 07:54:28

(动态规划)matrix -- hdu -- 5569的相关文章

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

2014 ACM/ICPC Asia Regional Xi&#39;an Online 233 Matrix,hdu 5015

比赛的时候若是这题过了就进前50 刚开始的时候大家的思路都以为是找规律的题目,于是再推公式,此外还发现类似于杨辉三角.于是又去套杨辉三角的通项去求. 于是TLE了无数次.(每次取范围的最大值也要3s多). 对于明显的矩阵样子,其实可以转化为矩阵的运算,每一行的转移.就是对一个转移矩阵的幂运算.然后再用快速矩阵幂即可. A: 10 0 0 1 10 1 0 1 10 1 1 1 0  0  0 1 B: 23 0 0 3 C=A^M  *B,ans=C[N] 教训:对于时间限制,即便是最大数据也要

hdu 2686 Matrix &amp;&amp; hdu 3367 Matrix Again (最大费用最大流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1394    Accepted Submission(s): 758 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number

hdu 5569 matrix dp

matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5569 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 wa

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

【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+M-1)ΣN+M-1(Ai-Aavg)2最小.Aavg为平均值. (N,M<=30,矩阵里的元素0<=C<=30) 题目思路: [动态规划] 首先化简式子,得原式=(N+M-1)ΣN+M-1(Ai2)-(ΣN+M-1Ai)2 f[i][j][k]表示走到A[i][j]格子上,此时前i+j-1

【动态规划】HDU 5791 Two

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5791 题目大意: A,B两个数列,问A的子集和B的子集相等的子集对数.子集内顺序按照数列顺序,相同的数字视为不同. 题目思路: [动态规划] f[i][j]表示A前i个数,B前j个数且第j个数必取的值.g[i][j]表示j不一定必取得值. ans=∑f[n][j]. 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<ios

【动态规划】HDU 5781 ATM Mechine

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 题目大意: 一个人有[0,K]内随机的钱,每次可以随意取,但是不知道什么时候取完,取钱超过剩余额度会警告一次,最多警告不能超过W.求期望取出钱的次数. 题目思路: [动态规划] 二分居然错了...看来二分出的答案不一定最优..起码第三个样例过不去. f[i][j]表示钱在[0,i]区间内,警告次数不超过j的期望取钱次数.那么取一次钱k相当于把钱分成两块,[0,k]和[k+1,i],即[0,k

【动态规划】HDU 1081 &amp; XMU 1031 To the Max

题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1031 题目大意: 给一个n*n(n<=100)的矩阵,求一个矩形覆盖的值最大是多少. 题目思路: [动态规划] 二维的最大字段和.先考虑一维的情况.f[i]=max(f[i-1]+a[i],a[i]) 只要之前的部分和大于零则一起取一定比只取当前位置的要优. 因此只要判断局部段的和是否大于零.同时每多取一个数就要更新答案. 之后只要将一维扩展到二维就行.枚举行的始末位置. 1 //