hdu5492_枚举dp

题目大意:给N*M(1<=N,M<=30)的矩阵,矩阵的每一格有一个非负权值(<=30)

从(1,1)出发,每次只能向右或向下移动,到达(n,m)时,经过的格子的权值形成序列A,

求(N+M−1)∑N+M−1i=1(Ai−Aavg)2

的最小值。

参考链接:http://blog.csdn.net/u014679804/article/details/48769267

将式子展开后,化简整理可得:(N+M-1)*s1-s2。其中s1是序列A的平方和,s2是序列A的和的平方。

注意到序列A的和不会超过(30+30-1)*30。

设dp[i][j][k]表示到达(i,j),序列和为k时,序列的平方和的最小值。

那么很容易得到状态转移方程,对于向右走,有:dp[i][j+1][k+V[i][j+1]]=min(dp[i][j+1][k+V[i][j+1]],dp[i][j][k]+V[i][j+1]*V[i][j+1]),V[i][j]为(i,j)的值,类似地,可得到向下走的状态转移方程。

最终,枚举(n+m-1)*dp[n][m][k]-k^2,0<=k<=59*30,取最小值即为答案。

注意dp的初始化,枚举序列A的和的时候,有些和是不会出现的,即有些状态是无法到达的。因此将dp全部初始化为inf,第(1,1)格的值初始化为V[1][1]^2,然后求解各个状态的值。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define inf 0x3f3f3f3f
14 typedef long long LL;
15
16 int dp[40][40][1800], a[40][40];
17 int main()
18 {
19     int t, n, m;
20     scanf("%d", &t);
21     for(int ca = 1; ca <= t; ca++)
22     {
23         scanf("%d %d", &n, &m);
24         for(int i = 1; i <= n; i++)
25             for(int j = 1; j <= m; j++)
26                 scanf("%d", &a[i][j]);
27         memset(dp, inf, sizeof(dp));
28         dp[0][1][0] = 0;
29         for(int i = 1; i <= n; i++)
30         {
31             for(int j = 1; j <= m; j++)
32             {
33                 for(int k = a[i][j]; k <=59 * 30; k++)
34                 {
35                     if(dp[i-1][j][k-a[i][j]] != inf)
36                         dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k-a[i][j]]+a[i][j]*a[i][j]);
37                     if(dp[i][j-1][k-a[i][j]] != inf)
38                         dp[i][j][k]=min(dp[i][j][k],dp[i][j-1][k-a[i][j]]+a[i][j]*a[i][j]);
39                 }
40             }
41         }
42         int res = inf;
43         for(int i = 0; i <= 59 * 30; i++)
44         {
45             if(dp[n][m][i] != inf)
46                 res = min(res, (n+m-1)*dp[n][m][i]-i*i);
47         }
48         printf("Case #%d: %d\n", ca, res);
49     }
50     return 0;
51 }

时间: 2024-10-13 01:34:16

hdu5492_枚举dp的相关文章

HDOJ 5303 Delicious Apples 枚举+DP

暴力枚举+DP 虽然是在环上,但最多只需要走一圈... dp[0][i]表示从1...i从起点逆时针走取完i个的花费,有 dp[0][i]=dp[0][i-k]+dist[i]*2 dp[1][i]表示从i...n从起点顺时针走取完n-i+1个的花费 dp[1][i]=dp[1][i+k]+(L-dist[i])*2 枚举哪些点顺时针哪些点逆时针: ans=min(ans,dp[0][i]+dp[1][i+1]); 然后枚举从哪个点开始走一圈:ans=min(ans,dp[0][max(0,i-

UvaLive 6661 Equal Sum Sets 二进制枚举/DP

链接:http://vjudge.net/problem/viewProblem.action?id=49406 题意:根据给出的n,k,s求出n个数每个数都不大于k,和为s的序列(n个数每个都不同)的总情况数. 思路: 1.二进制枚举枚举出所有可能排列,并求和若和为s,则符合,否则不符合. 代码: #include<iostream> #include<set> #include<map> #include<queue> #include<cstri

POJ:1050(枚举 + DP)

1 #include <stdio.h> 2 #include <math.h> 3 #include <iostream> 4 #include <string.h> 5 #include <algorithm> 6 using namespace std; 7 #define N 105 8 int f[N][N]; 9 int sum[N][N]; 10 int main(){ 11 int n; 12 while(scanf("

POJ 1050 To the Max 枚举+dp

大致题意: 求最大子矩阵和 分析: 一开始想复杂了,推出了一个状态方程:d[i][j]=max(d[i][j-1]+…,d[i-1][j]+…).写着写着发现上式省略的部分记录起来很麻烦. 后来发现n最大100,干脆直接枚举行,先枚举所有行的情况,然后将矩阵压缩为数列,最后用最大子段和求解.写着写着感觉就会超时,毕竟出现了四层循环嵌套.结果过了,说明测试数据有点水. #include <iostream> #include <cstdio> #include <cmath&g

【HDU3341】AC自动机状态压缩DP,或者说hash枚举DP,-------出题人卡常数都是狗!!!!!

题意:给若干种个串,再给个主串,然后把主串打乱顺序,使得包含子串尽量多(一种可以有多个,两个之间可以部分重叠).如第一组数据,ACGT,包含AC.CG.GT,三个,输出3.第二组数据A1A2A3,包含A1A2和A2A3两个"AA",答案为2. 其实我并没有AC.我被卡常数TLE了...实在不想写这种没意义的东西了. 贴代码,待填坑. #include <queue> #include <cstdio> #include <cstring> #incl

hdu4003 树形dp+分组背包

http://acm.hdu.edu.cn/showproblem.php?pid=4003 Problem Description Humans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots on

POJ 3311 Hie with the Pie (Floyd + 状压dp 简单TSP问题)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5019   Accepted: 2673 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

LightOJ 1422 Halloween Costumes (区间dp 好题)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, nextweekend is Halloween, and he is planning to attend as many parties as he can.Since it's Hall

POJ1699:Best Sequence(DP)

Description The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently we know that a gene is made of DNA. The nucleotide