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 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.
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.
必选的条件有点难度,若必选的话,那么dp[i][j]由dp[i][j-v[k]]和dp[i-1][j-v[k]]转移即 在第i组中去掉v[k]体积和在前i-1组中去掉v[k]体积,在取max时不能用max(dp[i-1][j],dp[i-1][j-v[k]]+w[k]),因为这种max的意思是这组可能不选任何一个,所以需要用max(dp[i][j],dp[i-1][j-v[k]+w[k])。
1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <iostream>
5 #include <vector>
6 #include <queue>
7 #include <cmath>
8 #include <set>
9 using namespace std;
10
11 #define N 105
12
13 int max(int x,int y){return x>y?x:y;}
14 int min(int x,int y){return x<y?x:y;}
15 int abs(int x,int y){return x<0?-x:x;}
16
17
18 struct node{
19 int v, w;
20 };
21
22 int dp[15][10005];
23 vector<node>ve[15];
24 int n;
25
26 main()
27 {
28 int i, j, k;
29 node p;
30 int V;
31 while(scanf("%d %d %d",&n,&V,&k)==3){
32 for(i=0;i<=k;i++) ve[i].clear();
33 for(i=0;i<n;i++){
34 scanf("%d %d %d",&j,&p.v,&p.w);
35 ve[j].push_back(p);
36 }
37 memset(dp,-1,sizeof(dp));
38 memset(dp[0],0,sizeof(dp[0]));
39 for(i=1;i<=k;i++){
40 for(j=0;j<ve[i].size();j++){
41 p=ve[i][j];
42 for(int v=V;v>=p.v;v--){
43 if(dp[i][v-p.v]!=-1) dp[i][v]=max(dp[i][v],dp[i][v-p.v]+p.w);
44 if(dp[i-1][v-p.v]!=-1) dp[i][v]=max(dp[i][v],dp[i-1][v-p.v]+p.w);
45 }
46 }
47 }
48 if(dp[k][V]==-1) printf("Impossible\n");
49 else printf("%d\n",dp[k][V]);
50 }
51 }