分组背包

F - I love sneakers!
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.

There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice.
Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.
 

Input

Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.
 

Output

For each test case, print an integer which is the maximum total value of the sneakers that Iserlohn purchases. Print "Impossible" if Iserlohn's demands can’t be satisfied.
 

Sample Input

5 10000 3 1 4 6 2 5 7 3 4 99 1 55 77 2 44 66
 

Sample Output

255
 1 Home    Problems    Status    Contest    [xiong_tao]    Logout
 2 xiong_tao 's source code for F
 3 Memory: 4908 KB         Time: 265 MS
 4 Language: G++         Result: Accepted
 5 
 6 1
 7 2
 8 3
 9 4
10 5
11 6
12 7
13 8
14 9
15 10
16 11
17 12
18 13
19 14
20 15
21 16
22 17
23 18
24 19
25 20
26 21
27 22
28 23
29 24
30 25
31 26
32 27
33 28
34 29
35 
36 #include<cstdio>
37 #include<string.h>
38 using namespace std;
39 int p[120],m[120],v[120],dp[120][10010];
40 int max1(int x,int y,int z)
41 {
42     x=x>y?x:y;
43     x=x>z?x:z;
44     return x;
45 }
46 int main()
47 {
48     int n,d,k;
49     int i,j,a;
50     while(scanf("%d%d%d",&n,&d,&k)!=EOF)
51     {
52         for(i=0; i<n; i++)
53             scanf("%d%d%d",&p[i],&m[i],&v[i]);
54         memset(dp,0,sizeof(dp));
55         for(i=1; i<=k; i++)
56             for(j=0; j<n; j++)
57                 for(a=d; a>=0; a--)
58                     if(a>=m[j]&&p[j]==i)
59                         dp[i][a]=max1(dp[i-1][a-m[j]]+v[j],dp[i][a],dp[i][a-m[j]]+v[j]);
60         if(dp[k][d]) printf("%d
",dp[k][d]);
61         else printf("Impossible
");
62     }
63     return 0;
64 }
65 
66 FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
67 All Copyright Reserved ©2010-2012 HUST ACM/ICPC TEAM
68 Anything about the OJ, please ask in the forum, or contact author:Isun
69 Server Time: 2014-07-28 20:21:39
原文地址:https://www.cnblogs.com/angledamon/p/3873927.html