首页 > 代码库 > 分组背包

分组背包

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 31 4 62 5 73 4 991 55 772 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 410 511 612 713 814 915 1016 1117 1218 1319 1420 1521 1622 1723 1824 1925 2026 2127 2228 2329 2430 2531 2632 2733 2834 2935 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\n",dp[k][d]);61         else printf("Impossible\n");62     }63     return 0;64 }65 66 FAQ | About Virtual Judge | Forum | Discuss | Open Source Project67 All Copyright Reserved ©2010-2012 HUST ACM/ICPC TEAM68 Anything about the OJ, please ask in the forum, or contact author:Isun69 Server Time: 2014-07-28 20:21:39