I love sneakers!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2857 Accepted Submission(s): 1169
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.
![](https://i-blog.csdnimg.cn/blog_migrate/ef35afe76885c46ac5bbfbb33553acea.jpeg)
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题意:有n个鞋子,有k个种类,每个鞋子都属于一个种类,每个鞋子都有一个价格和价值,现有金钱m元。要求使用这m元买鞋子能获得的最高价值,并要求每一个种类的鞋子中至少买一个,如果不能符合要求,则输出Impossible。思路:分组背包,在每组中进行01背包即可。关键是要知道是否每一个种类的鞋子都买到至少一个,我们可以设dp[i][j]为前i组用了j的金钱所获得的最大价值。初始化dp[i][j]为-1,只有前一组dp[i-1][]取到了鞋子(dp[i-1][]!=-1),才能更新当前组dp[i][]。最后若dp[k][m]!=-1,则表明所有的组都取到了鞋子。AC代码:#include <cstring> #include <string> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <vector> #include <cstdlib> #include <iostream> #define max2(a,b) ((a) > (b) ? (a) : (b)) #define min2(a,b) ((a) < (b) ? (a) : (b)) using namespace std; typedef struct node { int v,w; } shoe; vector<shoe>brand[15]; int main() { int dp[15][10005]; int n,m,k,a; shoe s; while(cin>>n>>m>>k) { for(int i=0;i<=k;i++) brand[i].clear(); for(int i=0; i<n; i++) { cin>>a>>s.v>>s.w; brand[a].push_back(s); } memset(dp,-1,sizeof(dp)); memset(dp[0],0,sizeof(dp[0])); for(int i=1;i<=k;i++) for(int p=0;p<(int)brand[i].size();p++) for(int j=m;j>=brand[i][p].v;j--) { if(dp[i][j-brand[i][p].v]!=-1) dp[i][j]=max2(dp[i][j],dp[i][j-brand[i][p].v]+brand[i][p].w); //同一种类的鞋子能取多个 if(dp[i-1][j-brand[i][p].v]!=-1) dp[i][j]=max2(dp[i][j],dp[i-1][j-brand[i][p].v]+brand[i][p].w); } if(dp[k][m]>=0) cout<<dp[k][m]<<endl; else cout<<"Impossible"<<endl; } return 0; }