hdoj--5569--matrix(动态规划)
matrix
Total Submission(s): 509 Accepted Submission(s): 297
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?
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)
2 3
1 2 3
2 2 1
2 3
2 2 1
1 2 4
4
8
用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[n][m]);
}
return 0;
}
hdoj--5569--matrix(动态规划)的更多相关文章
- hdu 5569 matrix dp
matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5569 D ...
- 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 ...
- Kruskal HDOJ 4313 Matrix
题目传送门 题意:如何花最小的代价使得一棵树划分开且不含同类节点 分析:当一条边连接的左右集合同类点小于等于1,那么不用删除,将两个集合合并,要求最小代价,那么贪心思想将权值降序排序,删除后剩下的就是 ...
- [Hdoj] Fast Matrix Calculation
题面:http://acm.hdu.edu.cn/showproblem.php?pid=4965 题解:https://www.zybuluo.com/wsndy-xx/note/1153981
- (动态规划)matrix -- hdu -- 5569
http://acm.hdu.edu.cn/showproblem.php?pid=5569 matrix Time Limit: 6000/3000 MS (Java/Others) Memo ...
- hdoj 3376,2686 Matrix Again 【最小费用最大流】
题目:hdoj 3376 Matrix Again 题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次仅仅能向右或者向下,出了末尾点其它仅仅能走一次,不能交叉,每次走到一个格子拿走这个格子中 ...
- 【leet-code】542. 01 矩阵
题目描述 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...
- 输入N组父子对,求父子对所组成的二叉树的高度----17年某公司的笔试题
题目的大致意思如下: 输入N组数,一组数代表一个父子对(如,0 1,0代表父节点,1代表子节点),求这N组数所组成的二叉树的高度: 例如: 输入:6 0 1 0 2 1 3 1 4 2 5 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
随机推荐
- WinServer-IIS-URL重写
WEB平台安装程序在Windows Server里面才有,在WIN7里面是没有的 然后在安装一个URL重写工具 然后再设置各种规则 来自为知笔记(Wiz)
- PHP中对hmac_sha1签名算法的实现方法
最近研究网宿云文档API,其中用到了一种叫hmac_sha1的签名算法: HMAC-SHA1: HMAC是哈希运算消息认证码 (Hash-based Message Authentication Co ...
- UVA 1329 Corporative Network【并查集】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- 开源TT框架上的日志类
public class Logger { /** * log tag */ private String tagName = "MoGuLogger";// tag name / ...
- (转载)Android之有效防止按钮多次重复点击的方法(必看篇)
为了防止测试妹子或者用户频繁点击某个按钮,导致程序在短时间内进行多次数据提交or数据处理,那到时候就比较坑了~ 那么如何有效避免这种情况的发生呢? 我的想法是,判断用户点击按钮间隔时间,如果间隔时间太 ...
- codeforces 493 C Vasya and Basketball
题意:给出三分线的值d,分别有两支队伍,如果小于等于d,得2分,如果大于d,得三分,问使得a-b最大时的a,b 一看到题目,就想当然的去二分了----啥都没分出来---55555555 后来才知道不能 ...
- tp框架--------where("1")
今天看代码的时候看到一个令我难以理解的sql查询语句,这是tp框架里的 return $this->where("1")->order('ar_id desc')-&g ...
- NetworkX-画图
参考:https://blog.csdn.net/qq951127336/article/details/54586869 1.创建图 networkx有四种图 Graph .DiGraph.Mult ...
- EFcore笔记之创建模型
排除属性:NotMapped NotMapped:排除属性在CodeFirst的时候在数据库里不创建该属性 public class Blog { public int BlogId { get; ...
- (转)Hadoop Eclipse开发环境搭建
来源:http://www.cnblogs.com/justinzhang/p/4261851.html This document is from my evernote, when I was s ...