HDU5569(dp)
2015-12-07 10:04
176 查看
matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 598 Accepted Submission(s): 345
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)
Recommend
hujie
分析:和hihocoder第五周的的题类似,不过这个多了2种情况,对边界特判即可。dp[i][j]表示到达这一点的最小值,并且i+j必须是奇数。
代码:
#include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; long long dp[1005][1005]; int a[1005][1005]; int n,m; int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%d",&a[i][j]); } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if((i+j)%2==0) continue; if(i==1) { dp[i][j]=dp[1][j-2]+a[1][j-1]*a[1][j]; continue; } if(j==1) { dp[i][j]=a[i-1][1]*a[i][1]+dp[i-2][1]; continue; } if(j!=1&&i!=1) { dp[i][j]=min((long long)dp[i-1][j-1]+a[i-1][j]*a[i][j],(long long)dp[i-1][j-1]+a[i][j-1]*a[i][j]); if(i-2>=1) { dp[i][j]=min(dp[i][j],dp[i-2][j]+(long long)a[i-1][j]*a[i][j]); } if(j-2>=1) { dp[i][j]=min(dp[i][j],dp[i][j-2]+(long long)a[i][j-1]*a[i][j]); } } } } printf("%I64d\n",dp [m]); } }
相关文章推荐
- UVa 10776 - Determine The Combination
- 越狱源和好用的插件
- iOS开发之如何跳到系统设置里的WiFi界面
- iOS系列UI篇——UIControl
- MySQL存储过程
- iOS沙盒的文件操作
- php生成高清缩略图实例详解
- HR战略规划
- SQL通过经度和纬度(已知的一个点)来查询方圆4km内符合条件的信息
- localStorage、sessionStorage用法总结
- Ubuntu加载SSH服务并允许root用户登陆的解决办法
- 用DataTable 递归生成目录树!
- 圣思园java se培训总结(25-27)(单例模式,访问权限)
- sql语句查询经纬度范围
- java006多台打印机执行
- QtSoap开发web services客户端程序
- Hadoop教程(三)HDFS文件系统Shell命令
- Opencv配置VS2010,配置成功后如何成功使用完成vs调用opencv库
- android(27)(显示意图之打开浏览器)
- 百度API服务和风天气获取当前天气信息