Pick apples(动态规划,难点背包容量太大)

1、点击打开链接Pick apples

//每种物品的最大重量是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 kind 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 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
*/


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值