大容量背包问题

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 ;
}

  

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3305249.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值