hdu3033 I love sneakers!(分组背包+每组至少一个)

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2229    Accepted Submission(s): 910


Problem 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
 
题意:Iserlohn 想买鞋子,鞋子共有 K 种款式,每种款式至少买一双;
分析:d[i][k]=max(d[i][k] , d[i][k - s[i][j].c] + s[i][j].w , d[i-1][k-s[i][j].c] + s[i][j].w )
         d[i][k] 表示 买了1 至 i 种款式的鞋子,使用的钱是 k 产生的最大的价值是 d[i][k];
         s[i][j] 表示第 i 款鞋的第 j 双鞋。 
 
View Code
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<vector>
 5 #include<cstdio>
 6 #define MAXK 11
 7 #define MAXN 101
 8 #define MAXV 10001
 9 #define INF -0x7fffffff
10 
11 using namespace std;
12 
13 int d[MAXK][MAXV];
14 struct sneak
15 {
16     int c,w;
17 };
18 vector<sneak>s[MAXK];
19 
20 int max(int A,int B,int C)
21 {
22     return A>B?(A>C?A:C):(B>C?B:C);
23 }
24 
25 int main()
26 {
27     int N,V,K,i,j,k;
28     while(scanf("%d %d %d",&N,&V,&K)==3)
29     {
30         for(i=0; i<=K; i++)
31             s[i].clear();
32         sneak t;
33         for(i=0; i<N; i++)
34         {
35             scanf("%d %d %d",&j,&t.c,&t.w);
36             s[j].push_back(t);
37         }
38         bool flag=false;
39         for(i=1; i<=K; i++)
40         {
41             if(s[i].size()==0)
42             {
43                 flag=true;
44                 break;
45             }
46         }
47         if(flag)
48         {
49             printf("Impossible\n");
50             continue;
51         }
52 
53         for(i=1; i<=K; i++)
54             for(j=0; j<=V; j++) //j 一定要从 0 开始
55                 d[i][j]=INF;
56         for(i=0; i<=V; i++)
57             d[0][i]=0;
58 
59         for(i=1; i<=K; i++)
60             for(j=0; j<s[i].size(); j++)
61                 for(k=V; k>=s[i][j].c; k--)
62                 d[i][k]=max(d[i][k],d[i][k-s[i][j].c]+s[i][j].w,d[i-1][k-s[i][j].c]+s[i][j].w);
63                 //如果分开的话一定要注意顺序
64 
65         if(d[K][V]<0) printf("Impossible\n");
66         else printf("%d\n",d[K][V]);
67     }
68     return 0;
69 }
70 /*
71 4 5 2
72 1 2 3
73 1 3 2
74 2 2 3
75 2 3 2
76 
77 3 5 3
78 1 2 5
79 2 2 1
80 3 2 2
81 
82 3 5 3
83 1 0 5
84 2 0 1
85 3 0 2
86 */

 

posted @ 2012-08-21 23:07  mtry  阅读(1324)  评论(0编辑  收藏  举报