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 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
HINT
题意
给定n*m(n+m为奇数)的矩阵,从(1,1)走到(n,m)且只能往右往下走,设经过的数为a1,a2,..,a2k,贡献为a1*a2+a3*a4...+a2k-1*a2k,求最小贡献
题解:
dp[i][j]表示走到i,j的最小贡献,我们只考虑(i+j)为奇数的时候就好了
然后转移的时候,也只会从奇数位置转移过来
代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int mp[][];
int dp[][];
const int inf = 1e9+;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
dp[i][j]=inf;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
mp[i][j]=read();
dp[][]=,dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if((i+j)%)
{
dp[i][j]=inf;
if(i>&&j>)
dp[i][j]=min(dp[i][j],dp[i-][j-]+min(mp[i-][j]*mp[i][j],mp[i][j-]*mp[i][j]));
if(i>)
dp[i][j]=min(dp[i][j],dp[i-][j]+mp[i-][j]*mp[i][j]);
if(j>)
dp[i][j]=min(dp[i][j],dp[i][j-]+mp[i][j]*mp[i][j-]);
}
}
}
printf("%d\n",dp[n][m]);
}
}
hdu 5569 matrix dp的更多相关文章
- hdu 5569 matrix(简单dp)
Problem Description Given a matrix with n rows and m columns ( n+m ,) and you want to go to the numb ...
- HDU 5569 matrix
简单DP /* *********************************************** Author :Zhou Zhentao Email :774388357@qq.com ...
- (动态规划)matrix -- hdu -- 5569
http://acm.hdu.edu.cn/showproblem.php?pid=5569 matrix Time Limit: 6000/3000 MS (Java/Others) Memo ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- hdu 4507 数位dp(求和,求平方和)
http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...
- hdu 3709 数字dp(小思)
http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4283 区间dp
You Are the One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
随机推荐
- Tombstone crash
首先,android平台应用程序可能产生以下四种crash:App层:Force close crashANR crashNative层:Tombstone crashKernel层:Kernel p ...
- HDU 5679 Substring 后缀数组判重
题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...
- OpenGL超级宝典第5版&&glProvokingVertex
翻译:https://www.opengl.org/sdk/docs/man3/xhtml/glProvokingVertex.xml 方法原型:void glProvokingVertex(GLen ...
- 【转载】【内存对齐(二)】__declspec( align(#) )的用法和大小计算
转自:http://www.cppblog.com/deercoder/archive/2011/03/13/141747.html 感谢作者! 在上面讲到了关于pack的内存对齐和计算方法,这里继续 ...
- [LeetCode] Ugly Number (A New Question Added Today)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- 多校6 1010 HDU5802 Windows 10 dfs
// 多校6 1010 HDU5802 Windows 10 // 题意:从p到q有三种操作,要么往上升只能1步,要么往下降,如果连续往下降就是2^n, // 中途停顿或者向上,下次再降的时候从1开始 ...
- 多校5 1001 HDU5781 ATM Mechine 记忆化搜索+概率
// 多校5 1001 HDU5781 ATM Mechine // http://acm.hdu.edu.cn/search.php?field=problem&key=2016+Multi ...
- 未能加载Connector/NET :: v6.7.4
//从*.config文件获取连接字符串和提供程序 string dp = ConfigurationManager.AppSettings["provider"]; string ...
- Subversion 1.8.1编译安装(self)
Subversion 1.8中http客户端基于neon已经被移除,改用self.如果要支持http方式需要在安装svn前安装serf,安装serf推荐用serf-1.2.1,安装是./configu ...
- UVALive 7327 Digit Division (模拟)
Digit Division 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/D Description We are given ...