Problem descriptionafter 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. obviusly, 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.
Inputinput 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.
Outputfor each test case, print an integer which is the maximum total value of the sneakers that iserlohn purchases. Print "impossible" If iserlohn's demans can't be satisfied.
Sample Input
5 10000 31 4 62 5 73 4 991 55 772 44 66
Sample output
255
Q: K brands of shoes, at least one of each brand, and each brand must be selected. The greatest value is the question.
If you only look at the brand, it is the 01 backpack. For each brand, it is like a full backpack.
DP [I] [J] represents the maximum value produced when the previous I BRAND spent on J.
In the beginning, only the DP [0] line is 0, and other DP values are set to-1, because no brand value is valid.
DP [I] [J] = max (DP [I-1] [J-W [k] + V [K], DP [I] [J-W [k] + V [K], DP [I] [J]). This formula represents the first time I was selected, and have selected the I brand again to select the shoes, and their own maximum value.
#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;typedef long long LL;const int MAX=0x3f3f3f3f;int n,m,k,num[105],v[105],w[105],dp[105][10005];int Max(int a,int b,int c) { return max( max(a,b) , c );}int main(){ while(~scanf("%d%d%d",&n,&m,&k)) { memset(dp,-1,sizeof(dp)); memset(dp[0],0,sizeof(dp[0])); for(int i=1;i<=n;i++) scanf("%d%d%d",&num[i],&w[i],&v[i]); for(int i=1;i<=k;i++) for(int j=1;j<=n;j++) if( num[j] == i ) for(int p=m;p >= w[j];p--) dp[i][p] = Max( dp[i][p] ,dp[i-1][ p-w[j] ]+v[j] ,dp[i][ p-w[j] ]+v[j] ); if(dp[k][m] < 0) printf("Impossible\n"); else printf("%d\n",dp[k][m]); } return 0;}
Zookeeper