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个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
随机推荐
- Android Thread.UncaughtExceptionHandler捕获
在Java 的异常处理机制中:如果抛出的是Exception异常的话,必须有try..catch..进行处理,属于checked exception.如果抛出的是RuntimeException异常的 ...
- ecshop init.php文件分析
1. ecshop init.php文件分析 2. <?php 3. 4. /** 5. * ECSHOP 前台公用文件 6. * ===================== ...
- UVA 10047 The Monocycle
大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...
- POJ 1068 Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24932 Accepted: 14695 De ...
- codeforces 700B Connecting Universities 贪心dfs
分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献 ...
- VC++6.0 MFC播放视频
注:需要在windows xp下才可以使用Windows Media Player插件,在windows 7下面会找不到该插件. 1.Windows Media Player控件的主要方法: 1)Ge ...
- 仿酷狗音乐播放器开发日志二十五 duilib右键事件的不足的bug修复
转载请说明原出处,谢谢~~ 虽然仿酷狗的各个菜单早就写好了,但是一直没有附加到程序里.今天把菜单和播放列表控件关联时发现了问题. 和播放列表相关的菜单有三个,分别是每个音乐项目控件相关的菜单.分组的菜 ...
- 使用 svn://ip/filename 方式访问svn 资源库
a. 下载 SVN的官方网站为 http://subversion.tigris.org/,当前最新版本为1.4.2.Windows下的二进制安装包分为两种,一种是以setup结尾的安装文件,另一种是 ...
- 2016年VR&AR有市场吗?
新霸哥发现了近期虚拟现实(VR)和增强现实(AR)非常火,已经成为了科技产业中最具前景的技术之一.其实说起这两项技术的应用,人们最容易把它与电子游戏联系在一起,那么在2016年VR&AR有市场 ...
- Juicer javascript 模板引擎
模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. js模板引擎包括如下:template官方参考:http://au ...