hdoj--5569--matrix(动态规划)
2015-11-29 10:25
267 查看
matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 509 Accepted Submission(s): 297
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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)
[align=left]Output[/align]
For each cases, please output an integer in a line as the answer.
[align=left]Sample Input[/align]
2 3 1 2 3 2 2 1 2 3 2 2 1 1 2 4
[align=left]Sample Output[/align]
4 8
[align=left]Source[/align]
BestCoder Round #63 (div.2)
[align=left]Recommend[/align]
hujie | We have carefully selected several similar problems for you: 5589 5588 5587 5586 5585
用dp数组记录下到达每一个点最小的路径
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; long long dp[1010][1010],num[1010][1010]; int m,n; int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(num,0,sizeof(num)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%lld",&num[i][j]); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i==1&&j==1) dp[i][j]=0; else if((i+j)&1) { if(i==1) dp[i][j]=dp[i][j-1]+num[i][j-1]*num[i][j]; else if(j==1) dp[i][j]=dp[i-1][j]+num[i-1][j]*num[i][j]; else dp[i][j]=min(dp[i][j-1]+num[i][j-1]*num[i][j],dp[i-1][j]+num[i-1][j]*num[i][j]); } else { if(i==1) dp[i][j]=dp[i][j-1]; else if(j==1) dp[i][j]=dp[i-1][j]; else dp[i][j]=min(dp[i-1][j],dp[i][j-1]); } } } printf("%lld\n",dp [m]); } return 0; }
相关文章推荐
- Linux下文件的复制
- Codevs_P2859 丑数统计(堆)
- unbuntu server12.04配置hadoop2.7.1(一):ubantu常用命令与准备知识
- 1090. Highest Price in Supply Chain (25)
- 使用npm安装一些包失败了的看过来(npm国内镜像介绍)
- 人力资源管理平台首页雏形
- 黑马程序员—Java基础—面向对象
- SpringMVC中避免拦截css,js,html图片等静态文件
- 【CS】——C#初识
- 第一次安装oracle遇到的问题
- REDIS
- hdoj Visible Trees 2841 (容斥原理)
- 人人都应该懂点 TCP
- 异常处理的原则
- 字符串中的有效地址
- javascript实现拖动元素交换位置
- FPGA服务器硬件搭建涉及的知识点3
- Android:View Animation
- ubuntu下安装源代码索引工具OpenGrok
- 获取网络图片并下载到本地相册