Light oj 1200 - Thief 《完全背包》
来源:互联网 发布:php 伪造来路 编辑:程序博客网 时间:2024/05/17 22:06
A thief has entered into a super shop at midnight. Poor thief came here because his wife has sent him to buy some necessary households. Instead of buying the items, he decided to steal them.
He has a bag with him which can carry up to W kg. In the list (given by his wife) there are four fields 1) item name, 2) the price of the item, 3) how many of this item is required and 4) the weight of this item. The items are solid items, so he can't take any item after dividing into pieces.
Now the thief wants to take items in his bag such that it fulfills his wife's list, and the total weight of the items is not greater than W. And he can't take any item other than the items mentioned in the list, otherwise his wife would found the item and he may get caught. But he can take more items than required. And he wants to sell these extra items and earn some money. Assume that he can take any item (is in the list) any number of times unless they overflow his bag.
Now you are given the necessary information of the items and his bag, you have to find the maximum profit the thief can make.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 100) and W (1 ≤ W ≤ 10000), where n denotes the number of items. Each of the next n lines contains three integers pi ci wi (1 ≤ pi, ci, wi ≤ 100), meaning that the price of the ith item is pi, ci of it must be taken, and the weight is wi.
Output
For each case, print the case number and the maximum profit he can get. If it's impossible to fulfill his wife's requirements, print 'Impossible'.
Sample Input
Output for Sample Input
2
3 20
10 1 10
5 1 5
1 1 1
1 10
10 1 11
Case 1: 4
Case 2: Impossible
01背包与完全背包:
01背包每种物品值有一个-.-所以容量应该从大往小取,以满足每个容量状态下每种物品只有一个-.-
完全背包每种物品有无数多个-.-所以容量应该从小往大取,以满足每个容量状态下的取值都是最优值。
for (int i=0;i<n;i++)
for (int j=s;j>=wi[i];j--) // 01 背包
bao[j]=max(bao[j],bao[j-wi[i]]+pi[i]);
for (int i=0;i<n;i++)
for (int j=wi[i];j<=s;j++) // 完全背包
bao[j]=max(bao[j],bao[j-wi[i]]+pi[i]);
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int pi[200],wi[200];int bao[10010];int main(){int t;scanf("%d",&t);for (int ca=1;ca<=t;ca++){int n,s,kk;scanf("%d%d",&n,&s);for (int i=0;i<n;i++){scanf("%d%d%d",&pi[i],&kk,&wi[i]);s-=kk*wi[i];}if (s<0){printf("Case %d: Impossible\n",ca);continue;}if (s==0){printf("Case %d: 0\n",ca);continue;}memset(bao,0,sizeof(bao));for (int i=0;i<n;i++){int lp=s/wi[i];/*while (lp--) for (int j=s;j>=wi[i];j--)*/for (int j=wi[i];j<=s;j++)bao[j]=max(bao[j],bao[j-wi[i]]+pi[i]);}printf("Case %d: %d\n",ca,bao[s]);}return 0;}
- Light oj 1200 - Thief 《完全背包》
- lightoj 1200 - Thief 完全背包
- Light oj 1200 - Thief(水)
- Light oj 1249 - Chocolate Thief【水】
- Light OJ 1200 Theif (多重背包)
- codeforces632e Thief in a Shop(完全背包)
- Light OJ 1231(背包dp)
- Light OJ-----1097 01背包dp
- Light 1249-Chocolate Thief【模拟】
- 完全背包 南阳oj 311
- CodeForces 632E Thief in a Shop(DP|完全背包)
- light oj 1231-1232 - 1233- Coin Change 背包
- light oj 1147 背包dp+状态压缩 好题
- Light oj 1180 - Software Company(二分+背包)
- Light OJ 1079 Just another Robbery (概率+背包)
- Light OJ 1147 Tug of War (背包+状压)
- light oj 1079 - Just another Robbery 【01背包】
- 洛谷Oj-疯狂的采药-完全背包
- NPM小结
- net-snmp移植项目总结
- linux 查看系统信息命令(比较全)
- POJ2886 Who Gets the Most Candies?(反素数+线段树模拟约瑟夫环)
- Java 多线程学习笔记(九) join
- Light oj 1200 - Thief 《完全背包》
- 使用TensorFlow创建自己的手写识别引擎
- 使用git提交文件修改提交者姓名
- C++学习手记
- 试试
- csharp: Procedure with DAO and DAL
- 敏感词过滤与DFA算法Trie树
- Android事件总线EventBus的用法详解
- webvirtmgr推荐