HDU 2602 (0-1背包)

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 35815    Accepted Submission(s): 14753

Problem Description

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input

The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output

One integer per line representing the maximum of the total value (this number will be less than 231).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

Author

Teddy

Source

HDU 1st “Vegetable-Birds Cup” Programming Open Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  1203 2159 2955 1171 2191

题目大意:

  就是说,给你一个N和V和N个物品的v[i]和w[i],让你求出,并且每次向背包中放入一个物品,让你求出最多能放多少个物品(物品的体积<=V)所能带来的最大的价值。

解题思路:

  直接走0-1背包的模型:

    定义状态:dp[i+1][j]表示,从前i个物品中选出来总重不超过j的物品所能带来的最大价值.

    初始状态:dp[0][j]==0.

    状态转移方程: dp[i+1][j] = dp[i][j] ,当j < w[i]时。

            dp[i+1][j] = max ( dp[i][j],dp[i][j-w[i]]+v[i]); 其他

 1 # include<cstdio>
 2 # include<iostream>
 3
 4 using namespace std;
 5
 6 const int MAX = 1000+4;
 7
 8 int dp[MAX][MAX];
 9 int w[MAX];
10 int v[MAX];
11
12
13 int main(void)
14 {
15     int t;cin>>t;
16     while ( t-- )
17     {
18         int n,vv;
19         cin>>n>>vv;
20         for ( int i = 0;i < n;i++ )
21         {
22             cin>>v[i];
23         }
24         for ( int i = 0;i < n;i++ )
25         {
26             cin>>w[i];
27         }
28         for ( int i = 0;i < n;i++ )
29         {
30             for ( int j = 0;j <= vv;j++ )
31             {
32                 dp[0][j] = 0;
33                 if ( j < w[i] )
34                 {
35                     dp[i+1][j] = dp[i][j];
36                 }
37                 else
38                 {
39                     dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+v[i] );
40                 }
41             }
42         }
43         cout<<dp[n][vv]<<endl;
44     }
45
46
47     return 0;
48 }

代码:

时间: 2024-11-13 21:33:30

HDU 2602 (0-1背包)的相关文章

hdu 2602 Bone Collector 背包入门题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包  注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34192 Accepted Submission(s): 14066 Proble

hdu 2602 dp 01背包

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 这题是非常标准的01背包,没啥特殊的地方,很简单 代码: #include <bits/stdc++.h> #define MAXS 1006 using namespace std; int value[MAXS]; int main () { int T; int n,m_v,v; int f[MAXS] ; cin >> T; while(T--) { memset(f,

hdu 2602(01背包)

01背包:有N件物品和一个容量为V 的背包.放入第i件物品耗费的空间是Ci,得到 的价值是Wi.求解将哪些物品装入背包可使价值总和最大.(每件物品只有一件,放或者不放) 即F[i,v]表示前i件物品恰放入一个容量为v的背包可以 获得的最大价值.则其状态转移方程便是:F[i,v] = max{F[i−1,v],F[i−1,v−Ci] + Wi} 我们可以做一点空间上的优化,用F[v]表示当前v空间所获得的最大价值,则F[v]=max(F[v],F[v-Ci]+W[i]); 这里要注意一个问题,空间

HDU 2602 Bone Collector 0/1背包

题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28903    Accepted Submission(s): 11789 Problem Description Many years ago , in Teddy's h

hdu 2602 Bone Collector 【01背包模板】

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

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 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

HDU 4501 多维背包

小明系列故事--买年货 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2261 Accepted Submission(s): 1018 Problem Description 春节将至,小明要去超市购置年货,于是小明去了自己经常去的都尚超市. 刚到超市,小明就发现超市门口聚集一堆人.用白云女士的话说就是:"那家伙,那场面,真是人山人海,锣

HDU 2955 Robberies (01背包)

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

杭电1171 Big Event in HDU(母函数+多重背包解法)

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