HDU5569/BestCoder Round #63 (div.2) C.matrix DP

matrix

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

题解:令dp[i][j]dp[i][j]表示当前走到第i,ji,j个位置的最小贡献,我们可以假定(i+j)(i+j)为奇数,由该状态可以转移向最多44个位置,就可以了。

///1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <stack>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define meminf(a) memset(a,127,sizeof(a));

inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
    return x*f;
}
//****************************************
#define maxn 100000+50
#define mod 1000000007
#define inf 1000000007

ll a[1001][1001],b[maxn],k,dp[1001][1001],l[maxn],r[maxn],ans[maxn],D[maxn],n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                scanf("%I64d",&a[i][j]);dp[i][j]=inf;
            }
        }
        dp[1][1]=0;
       for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            if(i==1&&j==1) {
                 dp[i][j+1]=min(dp[i][j]+a[i][j]*a[i][j+1],dp[i][j+1]);
            dp[i+1][j] =min( dp[i][j]+a[i][j]*a[i+1][j],dp[i+1][j]);
            }
           else {
              dp[i][j+2]=min(dp[i][j]+a[i][j+1]*a[i][j+2],dp[i][j+2]);
            dp[i+2][j] =min( dp[i][j]+a[i+1][j]*a[i+2][j],dp[i+2][j]);
             dp[i+1][j+1]=min(dp[i][j]+a[i+1][j]*a[i+1][j+1],dp[i+1][j+1]);
            dp[i+1][j+1] =min( dp[i][j]+a[i][j+1]*a[i+1][j+1],dp[i+1][j+1]);
           }
        }
       }

        cout<<dp[n][m]<<endl;

    }

    return 0;
}

时间: 2024-10-12 00:33:45

HDU5569/BestCoder Round #63 (div.2) C.matrix DP的相关文章

BestCoder Round #81 (div.2) B Matrix

B题...水题,记录当前行是由原矩阵哪行变来的. 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 #include<queue> 6 #include<stack> 7 #include<cmath> 8 #include<algorithm> 9 #include<malloc.h>

HDU5567/BestCoder Round #63 (div.2) A sequence1 水

sequence1  Given an array a with length n, could you tell me how many pairs (i,j) ( i < j ) for abs(ai−aj) mod b=c. Input Several test cases(about 5) For each cases, first come 3 integers, n,b,c(1≤n≤100,0≤c<b≤109) Then follows n integers ai(0≤ai≤109

HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)

传送门 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 311    Accepted Submission(s): 142 Problem Description There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we

计算几何(水)BestCoder Round #50 (div.2) 1002 Run

题目传送门 1 /* 2 好吧,我不是地球人,这题只要判断正方形就行了,正三角形和正五边形和正六边形都不可能(点是整数). 3 但是,如果不是整数,那么该怎么做呢?是否就此开启计算几何专题了呢 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-8 19:54:14 8 * File Name :B.cpp 9 ************

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)

Problem Description After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to

BestCoder Round #11 (Div. 2) 前三题题解

题目链接: huangjing hdu5054 Alice and Bob 思路: 就是(x,y)在两个參考系中的表示演全然一样.那么仅仅可能在这个矩形的中点.. 题目: Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 216    Accepted Submission(s): 166 Problem De

BestCoder Round #11 (Div. 2) 题解

HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 302    Accepted Submission(s): 229 Problem Description Bob and Alice got separated in the Square, they agreed that if they

HDU 5651 xiaoxin juju needs help(BestCoder Round #77 (div.1)1001)

传送门 xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 243 Problem Description As we all known, xiaoxin is a brilliant coder. He knew **palin

BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 280 Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the