HDU 5234 Happy birthday 01背包

题目链接:

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

bc:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=585&pid=1003

题解:

由于数据比较小,所以可以转化为判定性问题,即:

  dp[i][j][kk]表示走到i,j这一点时吃了kk重的蛋糕,转移方程只要考虑这一点的蛋糕吃和不吃两种情况(01背包)

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5
 6 const int maxn=111;
 7
 8 bool dp[maxn][maxn][maxn];
 9 int arr[maxn][maxn];
10 int n,m,kilo;
11
12 void init(){
13     memset(dp,0,sizeof(dp));
14     for(int i=0;i<maxn;i++){
15         for(int j=0;j<maxn;j++){
16             dp[i][j][0]=1;
17         }
18     }
19 }
20
21 int main(){
22     while(scanf("%d%d%d",&n,&m,&kilo)==3&&n){
23         init();
24         for(int i=1;i<=n;i++){
25             for(int j=1;j<=m;j++){
26                 scanf("%d",&arr[i][j]);
27             }
28         }
29
30         for(int i=1;i<=n;i++){
31             for(int j=1;j<=m;j++){
32                 for(int k=1;k<=kilo;k++){
33                     //(i,j)这点的蛋糕不吃
34                     dp[i][j][k]=dp[i-1][j][k]|dp[i][j-1][k];
35                     //(i,j)吃这点的蛋糕
36                     if(k>=arr[i][j]){
37                         dp[i][j][k]|=dp[i-1][j][k-arr[i][j]];
38                         dp[i][j][k]|=dp[i][j-1][k-arr[i][j]];
39                     }
40                 }
41             }
42         }
43
44         int ans=0;
45         for(int i=kilo;i>=0;i--){
46             if(dp[n][m][i]){
47                 ans=i; break;
48             }
49         }
50         printf("%d\n",ans);
51     }
52     return 0;
53 }
时间: 2025-01-18 02:35:09

HDU 5234 Happy birthday 01背包的相关文章

HDU 2602 (简单的01背包) Bone Collector

很标准的01背包问题 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 int w[maxn], v[maxn], dp[maxn]; 9 10 int main(void) 11 { 12 #ifdef LOCAL 13 freopen(&qu

HDU 1171 Big Event in HDU(母函数或01背包)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 44151    Accepted Submission(s): 15191 Problem Description Nowadays, we all know that Computer College is the biggest department

hdu 3466 Proud Merchants(0-1背包+排序)

题目来源:hdu 3466 Proud Merchants Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3595 Accepted Submission(s): 1500 Problem Description Recently, iSea went to an ancient country. For

HDU 2546 饭卡(01 背包)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路:需要首先处理一下的的01背包,当饭卡余额大于等于5时,是什么都能买的,所以题目要饭卡余额最小,那预留5元(相当于饭卡余额为5)来买最贵的菜 然后对剩下n-1进行01背包dp才是正确的.但是还存在一个问题,那就饭卡初始余额小于5时,也要处理掉. 下面讲01背包(原型可以看大牛的背包九讲,本人也正在学习),定义dp[i][j]为买前i种菜品剩下j元时的最大消费值等于下面两中情况之一的值 有两种来

HDU 2546 饭卡(01背包裸题)

饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28562    Accepted Submission(s): 9876 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无

HDU 2602 Bone Collector(01背包裸题)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 60469    Accepted Submission(s): 25209 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bo

hdu 2546 饭卡(0-1背包)

题目来源:hdu 2546 饭卡 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16645 Accepted Submission(s): 5797 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后

hdu 1574 RP问题 01背包的变形

hdu 1574 RP问题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1574 分析:01背包的变形. RP可能为负,所以这里分两种情况处理一下就好. 初始化要注意. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define inf 0x3f3f3f3f int

hdu 2955 Robberies(01背包)

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17723    Accepted Submission(s): 6544 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that