您的位置:首页 > 其它

大容量背包问题

2013-09-06 12:57 260 查看

2197: Pick apples

Time Limit: 1 Sec Memory Limit: 128 MB

Description

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.

Input

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.

Output

For each case, first output the case number then follow the most profits she can gain.

Sample Input

1
1 1
2 1
3 1
6

Sample Output

Case 1: 6

HINT

Source

第三届山东ACM省赛

思路:大容量贪心,小容量DP背包。

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <vector>
#define Max(a,b) ((a)>(b)?(a):(b))
using namespace std ;
typedef long long LL ;
LL dp[2000008] ;
struct Me{
struct Apple{
LL  value ;
LL volume ;
friend bool operator <(const Apple A ,const Apple B){
return A.value*B.volume>B.value*A.volume ;
}
};
Apple apple[4] ;
LL V ;
Me(){} ;
void read(){
for(int i=1;i<=3;i++)
cin>>apple[i].volume>>apple[i].value ;
cin>>V ;
}
LL DP(int Volume){
fill(dp,dp+1+Volume,0) ;
for(int k=1;k<=3;k++)
for(int i=1;i<=Volume;i++){
dp[i]=Max(dp[i-1],dp[i]) ;
if(i>=apple[k].volume)
dp[i]=Max(dp[i],dp[i-apple[k].volume]+apple[k].value) ;
}
return dp[Volume] ;
}
LL gao(){
read() ;
sort(apple+1,apple+1+3) ;
if(V<1000000)
return DP(V) ;
LL ans=0 ;
LL n=(V-1000000)/apple[1].volume ;
ans+=n*apple[1].value ;
ans+=DP(V-n*apple[1].volume) ;
return ans ;
}
};
int main(){
int T ,k=1;
scanf("%d",&T);
while(T--){
Me me ;
printf("Case %d: ",k++) ;
cout<<me.gao()<<endl ;
}
return 0 ;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: