lightoj1200_完全背包二进制优化

http://lightoj.com/volume_showproblem.php?problem=1200

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 pici 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

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15
16 int p[110], c[110], w[110], dp[10005];
17 int main()
18 {
19     int t, n, W;
20     scanf("%d", &t);
21     for(int ca = 1; ca <= t; ca++)
22     {
23         scanf("%d %d", &n, &W);
24         for(int i = 1; i <= n; i++)
25         {
26             scanf("%d %d %d", &p[i], &c[i], &w[i]);
27             W -= c[i]*w[i];
28         }
29         if(W < 0)
30         {
31             printf("Case %d: Impossible\n", ca);
32             continue;
33         }
34         if(W==0)
35         {
36             printf("Case %d: 0\n", ca);
37             continue;
38         }
39         for(int i = 1; i <= n; i++)
40             c[i] = W / w[i];
41         memset(dp, 0, sizeof(dp));
42         for(int i = 1; i <= n; i++)
43         {
44             for(int j = 1; j <= c[i]; j <<= 1)
45             {
46                 for(int k = W; k >= j * w[i]; k--)
47                 {
48                     dp[k] = max(dp[k-j*w[i]]+j*p[i], dp[k]);
49                 }
50                 c[i] -= j;
51             }
52             if(c[i])
53             {
54                 for(int k = W; k >= c[i] * w[i]; k--)
55                 {
56                     dp[k] = max(dp[k-c[i]*w[i]]+c[i]*p[i], dp[k]);
57                 }
58             }
59         }
60         printf("Case %d: %d\n", ca, dp[W]);
61     }
62     return 0;
63 }
时间: 2024-11-05 11:42:23

lightoj1200_完全背包二进制优化的相关文章

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

[多重背包+二进制优化]HDU1059 Dividing

题目链接 题目大意: 两个人要把一堆宝珠,在不能切割的情况下按照价值平分,他们把宝珠分成6种价值,每种价值的宝珠n个. n<=200000 思考: 首先如果加和下来的价值是一个偶数 那么还分毛啊,直接gg. 之后多重背包二进制优化 转换为 01背包. 我们可以把价值 同时当做宝珠的空间和价值. 那么我们现在要求的是 在 空间为一半的情况下,能否找到价值为 一半的情况. 1 #include <cstdio> 2 #include <algorithm> 3 #include

14年省赛---多重部分和问题(多重背包+二进制优化)

1210: F.多重部分和问题 时间限制: 1 Sec  内存限制: 64 MB提交: 18  解决: 14 题目描述 有n种不同大小的数字,每种各个.判断是否可以从这些数字之中选出若干使它们的和恰好为K. 输入 首先是一个正整数T(1<=T<=100)接下来是T组数据 每组数据第一行是一个正整数n(1<=n<=100),表示有n种不同大小的数字 第二行是n个不同大小的正整数ai(1<=ai<=100000)第三行是n个正整数mi(1<=mi<=100000

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers

hdu1059 dp(多重背包二进制优化)

hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听过```不得不吐槽自己的知识面太窄```于是,我用了母函数写这题,母函数的做法并没有问题,但是由于这道题的数据很大,母函数轻轻松松就超时了,于是,我又很努力地在母函数循环的优化上面想出路,改改改,各种改之后依旧TLE,01背包的做法显然也是会超时的,DISCUSS里的母函数做法优化方式都是模上一个大

HDU 3591 (完全背包+二进制优化的多重背包)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2798    Accepted Submission(s): 972 Problem Description In the countr

POJ 1014 Dividing【多重背包+二进制优化】

大意: 价值1, 2, 3, ……, 6的物品分别a1, a2, ……, a5, a6件 问能否把这些物品分成两份,使其具有相同的价值(所有物品必须全部用上) 分析: 给个物品有多件,即多重背包 只要看能不能将这些物品拼成   总价值 的 一半就可以了 转化为01背包是用二进制优化,否则会超时 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std;

POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】

Language: Default Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63647   Accepted: 16488 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal

POJ 1276 Cash Machine 多重背包--二进制优化

点击打开链接 Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28337   Accepted: 10113 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount