hdu 3033 I love sneakers! 分组背包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3033

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.

题意描述:有n个物品,多种品牌,每个产品有三个属性a,b和c,分别代表品牌种类、价钱和价值。现在你有现金m和k个伙伴,要求为每一位伙伴至少买一件物品,求出得到的最大价值(编号为u的伙伴只能得到品牌种类为u的物品)。

算法分析:分组背包。dp[i][j]代表前 i 种物品、身上有j元获得的最大价值。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<vector>
 8 #include<queue>
 9 #define inf 0x7fffffff
10 using namespace std;
11 const int maxn=100+10;
12 
13 int n,m,k;
14 int dp[12][10000+10];
15 vector<pair<int,int> > vec[maxn];
16 
17 int main()
18 {
19     while (scanf("%d%d%d",&n,&m,&k)!=EOF)
20     {
21         int a,b,c;
22         for (int i=1 ;i<=n ;i++) vec[i].clear();
23         for (int i=1 ;i<=n ;i++)
24         {
25             scanf("%d%d%d",&a,&b,&c);
26             vec[a].push_back(make_pair(b,c));
27         }
28         memset(dp,-1,sizeof(dp));
29         memset(dp[0],0,sizeof(dp[0]));
30         int flag=0;
31 //        for (int i=1 ;i<=k ;i++)
32 //        {
33 //            int num=vec[i].size();
34 //            for (int j=0 ;j<num ;j++)
35 //                cout<<vec[i][j].first<<" "<<vec[i][j].second<<endl;
36 //            cout<<endl;
37 //        }
38         for (int i=1 ;i<=k ;i++)
39         {
40             int num=vec[i].size();
41             if (num==0) {flag=1;break; }
42             for (int u=0 ;u<num ;u++)
43             {
44                 int p=vec[i][u].first;
45                 for (int j=m ;j>=p ;j--)
46                 {
47 
48                     int value=vec[i][u].second;
49                     if (dp[i][j-p]!=-1)
50                     dp[i][j]=max(dp[i][j],dp[i][j-p]+value);
51                     if (dp[i-1][j-p]!=-1)
52                     dp[i][j]=max(dp[i][j],dp[i-1][j-p]+value);
53                 }
54             }
55         }
56         if (flag || dp[k][m]==-1) printf("Impossible\n");
57         else printf("%d\n",dp[k][m]);
58     }
59     return 0;
60 }

 

posted @ 2015-03-28 14:24  huangxf  阅读(316)  评论(0编辑  收藏  举报