hdu-5569matrix(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=5569;

题目意思:

从(1,1)点出发只能向右和向下走,到达(n,n)点时所得到的价值最小,

价值是Let the numbers you go through become an array a1,a2,...,a2k. The cost is a1∗a2+a3∗a4+...+a2k−1∗a2k,每个

格都有相应的价值ak.

思路:dp,状态转移方程是如果(i+j)%2==1-- dp[i][j]=min(dp[i-1][j]+a[i][j]*a[i-1][j],dp[i][j-1]+a[i][j-1]*a[i][j]);

如果(i+j)%2==0--dp[i][j]=min(dp[i-1][j],dp[i][j-1]);

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<stdlib.h>
 6 typedef long long LL;
 7 LL dp[1005][1005];
 8 LL a[1005][1005];
 9 using namespace std;
10 int main(void)
11 {
12     int n,i,j,k,p,q;
13     while(scanf("%d %d",&p,&q)!=EOF)
14     {
15         for(i=1; i<=p; i++)
16         {
17             for(j=1; j<=q; j++)
18             {
19                 scanf("%lld",&a[i][j]);
20             }
21         }
22         memset(dp,0,sizeof(dp));
23         for(i=1; i<=p; i++)//初始化边界当j为1时,dp[i][j]只能从dp[i-1][1]转移而来
24         {
25             if((i+1)%2==1)
26             {
27                 dp[i][1]=dp[i-1][1]+a[i][1]*a[i-1][1];
28             }
29             else dp[i][1]=dp[i-1][1];
30         }
31
32         for(i=1; i<=q; i++)//初始化边界当i为1时,dp[i][j]只能从dp[1][j-1]转移而来
33         {
34             if((i+1)%2==1)
35             {
36                 dp[1][i]=dp[1][i-1]+a[1][i-1]*a[1][i];
37             }
38             else dp[1][i]=dp[1][i-1];
39         }
40         for(i=2; i<=p; i++)
41         {
42             for(j=2; j<=q; j++)
43             {
44                 if((i+j)%2==1)
45                     dp[i][j]=min(dp[i-1][j]+a[i][j]*a[i-1][j],dp[i][j-1]+a[i][j-1]*a[i][j]);
46                 else dp[i][j]=min(dp[i-1][j],dp[i][j-1]);
47             }
48         }
49         printf("%d\n",dp[p][q]);
50     }
51     return 0;
52 }
时间: 2024-10-11 06:19:51

hdu-5569matrix(dp)的相关文章

HDU 4832(DP+计数问题)

HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行,然后相乘累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long ll; const ll MOD = 9999991; const int N = 1005; int t, n, m, k, x, y; ll dp1

hdu 3944 dp?

DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 1804    Accepted Submission(s): 595 Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,-a

hdu 5389 dp类似背包

http://acm.hdu.edu.cn/showproblem.php?pid=5389 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft. Stilwell is enjoying the first chapter of this

hdu 1025 dp 最长上升子序列

1 //Accepted 4372 KB 140 ms 2 //dp 最长上升子序列 nlogn 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 500005; 8 int dp[imax_n]; 9 int d[imax_n]; 10 int a[imax_n]; 11 int n; 12 int len

HDU 5928 DP 凸包graham

给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也就是 dp[j][k]代表当前链末端为j,其内部点包括边界数量为k的最小长度.这样最后得到的一定是最优的凸包. 然后就是要注意要dp[j][k]的值不能超过L,每跑一次凸包,求个最大的点数量就好了. 和DP结合的计算几何题,主要考虑DP怎么搞 /** @Date : 2017-09-27 17:27

HDU 4901 DP背包

给你n个数,问你将数分成两个数组,S,T ,T 中所有元素的需要都比S任意一个大,问你S中所有元素进行 XOR 操作和 T 中所有元素进行 &操作值相等的情况有多少种. DP背包思路 dpa[i][j][0]  表示从左开始到i,不取i,状态为j的方案数 dpa[i][j][1]  表示从作开始到i,取i,状态为j的方案数 dpb[i][j]      表示从右开始到i,状态为j的方案数 因为S集合一定在T集合的左边,那么可以枚举集合的分割线,并且枚举出的方案要保证没有重复,如果要保证不重复,只

Bone Collector II(HDU 2639 DP)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3471    Accepted Submission(s): 1792 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par

hdu 4472 dp

http://acm.hdu.edu.cn/showproblem.php?pid=4472 第一直觉找规律,第二直觉 把树拆成子树,然后递推或者DP 然后发现不行,然后才发现自己题都没读,,,, dp[i]=segma(dp[j] | (i-1)%j==0) #include <cstdio> #include <cstring> #include <cstring> const int MAXN = 1000+50; const int MOD = 1e9+7; #

hdu 5791 (DP) Two

hdu 5791 Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1421    Accepted Submission(s): 630 Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequ

HDU 1069&amp;&amp;HDU 1087 (DP 最长序列之和)

H - Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1087 Appoint description:  System Crawler  (2015-11-18) Description Nowadays, a kind of chess game called “Su