HDU-5569 matrix(DP)
来源:互联网 发布:天谕捏脸数据男妖娆 编辑:程序博客网 时间:2024/06/05 00:30
matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)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 followsn lines with m numbers ai,j(1≤ai≤100)
For each cases, first come 2 integers,
n+m is an odd number.
Then follows
Output
For each cases, please output an integer in a line as the answer.
Sample Input
2 31 2 32 2 12 32 2 11 2 4
Sample Output
48
很快就想到了DP,题真不是白做的
对于任意i,j,且i+j为奇数时,dp[i][j]只能从dp[i-2][j],dp[i-1][j-1],dp[i][j-2]转移过来。
其中:
①a[i-2][j]只能经过a[i-1][j]到达a[i][j],状态转移方程为:dp[i][j]=min(dp[i][j],dp[i-2][j],a[i-1][j]*a[i][j]);
②a[i][j-2]只能经过a[i][j-1]到达a[i][j],状态转移方程为:dp[i][j]=min(dp[i][j],dp[i][j-2],a[i][j-1]*a[i][j]);
③a[i][j]可分别经过a[i][j-1]和a[i-1][j]到达a[i][j],状态转移方程为:
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+a[i][j]*min(a[i-1][j],a[i][j-1]));
合并后可得:
dp[i][j]=min(dp[i][j],min(min(dp[i-1][j-1],dp[i-2][j])+a[i][j]*a[i-1][j],min(dp[i-1][j-1],dp[i][j-2])+a[i][j]*a[i][j-1]));
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[1005][1005],dp[1005][1005],n,m;int main() { int i,j; while(scanf("%d%d",&n,&m)==2) { memset(dp,0x3f,sizeof(dp)); dp[0][1]=dp[1][0]=0; for(i=1;i<=n;++i) for(j=1;j<=m;++j) scanf("%d",&a[i][j]); for(i=2;i<=n;++i)//先处理第1列,防止越界 dp[i][1]=dp[i-2][1]+a[i][1]*a[i-1][1]; for(j=2;j<=m;++j)//先处理第1行,防止越界 dp[1][j]=dp[1][j-2]+a[1][j]*a[1][j-1]; //没想到用“我为人人”型转移状态,这样就可以直接处理,不用担心越界了 for(i=2;i<=n;++i) for(j=2;j<=m;++j) if((i+j)&1) dp[i][j]=min(dp[i][j],min(min(dp[i-1][j-1],dp[i-2][j])+a[i][j]*a[i-1][j],min(dp[i-1][j-1],dp[i][j-2])+a[i][j]*a[i][j-1])); printf("%d\n",dp[n][m]); } return 0;}
0 0
- HDU-5569 matrix(DP)
- hdu 5569 matrix (dp)
- hdu 5569 matrix(dp)
- HDU 5569 matrix(DP)
- hdu 5569 matrix(dp)
- hdu-5569-matrix-dp
- hdu 5569 matrix【dp】
- hdu 2686 Matrix ( 多进程DP)
- hdu 2686 Matrix(双线程DP)
- HDu 2830 Matrix Swapping II(dp)
- HDU 2686 Matrix(双线程DP)
- HDU-2830-Matrix Swapping II(DP)
- Matrix - HDU 2686 dp
- hdu 4313 Matrix 树形dp
- hdu 4313 Matrix 树形dp
- HDU 2686 Matrix (多线程DP)
- hdu 4313 Matrix(树形DP)
- hdu 2686 Matrix (多线程dp)
- muduo_base 源码分析:Timestamp
- AngularJS 入门学习
- Android面试题
- 编译使用Android源码中的GIF图片工具包--framesequence
- 已知路径,将图片通过imageview展示
- HDU-5569 matrix(DP)
- Linux下ffmpeg的完整安装
- JavaSE__获取Java程序运行的路径 | 获取当前jar包的路径
- HDU 3951 Coin Game(博弈取对称思路)
- hdoj 5569 matrix 【裸dp】
- 用函数指针代替switch-case
- js实现省份、城市的级联选择
- 机器学习资料
- bash手册