Pick apples(动态规划,难点背包容量太大)
2013-02-25 19:48
197 查看
1、点击打开链接Pick apples
//每种物品的最大重量是100,要求的这个数m应该是三种物品重量的最小公倍数
2、题目大意:
有三种苹果,每种苹果都有其重量和价值,且苹果数量都是无限个,给定一个容量为m的背包,求这个包装的苹果的价值最大是多少
3、简单的完全背包的问题,只是题目背包容量太大,需要找一个临界值,此值即从此值往后,只装价值和重量比值最大的,每种物品的最大重量是100,要求的这个数m应该是三种物品重量的最小公倍数
4、题目:
of apple has a size and a price to be sold. Now the little girl wants to gain more profits, but she does not know how. So she asks you for help, and tell she the most profits she can gain.
In each case, there are four lines. In the first three lines, there are two integers S and P in each line, which indicates the size (1 <= S <= 100) and the price (1 <= P <= 10000) of this kind of apple.
In the fourth line there is an integer V,(1 <= V <= 100,000,000)indicates the volume of the girl's bag.
5、代码:
//每种物品的最大重量是100,要求的这个数m应该是三种物品重量的最小公倍数
2、题目大意:
有三种苹果,每种苹果都有其重量和价值,且苹果数量都是无限个,给定一个容量为m的背包,求这个包装的苹果的价值最大是多少
3、简单的完全背包的问题,只是题目背包容量太大,需要找一个临界值,此值即从此值往后,只装价值和重量比值最大的,每种物品的最大重量是100,要求的这个数m应该是三种物品重量的最小公倍数
4、题目:
Pick apples
Time Limit: 1000MS Memory limit: 165536K
题目描述
Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each kindof apple has a size and a price to be sold. Now the little girl wants to gain more profits, but she does not know how. So she asks you for help, and tell she the most profits she can gain.
输入
In the first line there is an integer T (T <= 50), indicates the number of test cases.In each case, there are four lines. In the first three lines, there are two integers S and P in each line, which indicates the size (1 <= S <= 100) and the price (1 <= P <= 10000) of this kind of apple.
In the fourth line there is an integer V,(1 <= V <= 100,000,000)indicates the volume of the girl's bag.
输出
For each case, first output the case number then follow the most profits she can gain.示例输入
11 12 13 16
示例输出
Case 1: 6
5、代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; long long dp[1110]; long long w[5],v[5]; struct node { double p; int idx; } a[5]; int cmp(node a,node b) { return a.p>b.p; } int main() { int t; long long m; scanf("%d",&t); for(int cas=1; cas<=t; cas++) { memset(dp,0,sizeof(dp)); for(int i=1; i<=3; i++) { scanf("%lld%lld",&w[i],&v[i]); a[i].p=(v[i]*1.0)/(w[i]*1.0); a[i].idx=i; } scanf("%lld",&m); sort(a+1,a+1+3,cmp); if(m<=1000000)//每种物品的最大重量是100,要求的这个数m应该是三种物品重量的最小公倍数
{ for(long long i=1; i<=m; i++) { for(long long j=1; j<=3; j++) if(i>=w[j]) dp[i]=max(dp[i],dp[i-w[j]]+v[j]); } printf("Case %d: %lld\n",cas,dp[m]); } else { long long num=(m-1000000)/w[a[1].idx]; long long sum=num*v[a[1].idx]; m=m-num*w[a[1].idx]; for(long long i=1; i<=m; i++) { for(long long j=1; j<=3; j++) { if(i>=w[j]) dp[i]=max(dp[i],dp[i-w[j]]+v[j]); } } printf("Case %d: %lld\n",cas,dp[m]+sum); } } return 0; } //第二个样例即可验证是否需要if单独处理 /* 2 1 1 2 1 3 1 6 3 7 2 4 3 5 7 */
相关文章推荐
- 动态规划问题 -- 背包问题 单价不等水果 装入固定容量背包 最大价值问题
- 动态规划____背包(有容量 物品无限制)
- 2.7动态规划的背包问题
- HDU 2546:饭卡(动态规划:01背包)
- 动态规划8_多重背包
- 动态规划之0-1背包问题(简单易懂)
- 动态规划之01背包
- 0-1背包问题的动态规划求法(开心的小明)
- 动态规划求解01背包相关的基本问题
- 动态规划之背包问题
- 动态规划-完全背包问题
- 动态规划之0-1背包问题
- 算法-0-1背包的动态规划,回溯,分支限界三种解法
- 动态规划之背包问题(一)
- 动态规划-背包问题
- 动态规划之背包问题01——Java实现
- 背包算法递归实现,递归转动态规划的一般方法java实现
- 动态规划问题(背包问题)
- 动态规划---0-1背包问题
- 关于0-1背包的动态规划,回溯和分支限界法的一些分析和代码