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
*/