大容量背包问题
2013-09-06 12:57
260 查看
2197: Pick apples
Time Limit: 1 Sec Memory Limit: 128 MBDescription
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
11 1
2 1
3 1
6
Sample Output
Case 1: 6HINT
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 ; }
相关文章推荐
- 动态规划-背包问题(给定容量获得最大的价值量)2
- 动态规划-背包问题(给定容量获得最大的价值量)1
- 动态规划问题 -- 背包问题 单价不等水果 装入固定容量背包 最大价值问题
- LintCode-背包问题(01背包问题)
- NYOJ背包问题
- [经典] 背包问题(三)
- 简单的背包问题
- 经典背包问题 01背包+完全背包+多重背包
- 背包问题
- HDU 2955 Robberies(0-1背包问题的简单变形)
- 01背包问题
- nyoj 106 背包问题(简单贪心)
- 物品无限的背包问题
- 0-1背包问题(待续)
- 零崎的补番计划Ⅱ(0-1背包问题)
- 01背包问题
- 背包问题
- 简单的背包问题(入门)HDU2602 HDU2546 HDU1864
- 【算法设计与分析基础】背包问题
- 背包问题