T
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
题目要求对同属于一个组的物品只能取一个,将物品的价值和重量存储在带有序号的二维数组中,做三重循环i—k序数 j—同一组最多的个数 背包空间m-----当前物品的重量。
f[i][k]=MAX(f[i][k],f[i][k-w[i][j]]+v[i][j],f[i-1][k-w[i][j]]+v[i][j]);
遵从不取,取一个,取任意个来列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<vector>
#include<set>
#include<cstring>
#include<cstdlib>
#include<time.h>
#include<stack>
#define INF 0x3f3f3f3f
using namespace std;
#define maxn 10005
int f[15][maxn];
int w[15][maxn],v[15][maxn];//重量 价值
int MAX(int a,int b,int c){
int d=max(a,b);
return max(d,c);
}
int main()
{
int n,m,k,a,b,c;
int cnt[15];
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++){
scanf("%d%d%d",&a,&b,&c);
w[a][cnt[a]]=b;
v[a][cnt[a]++]=c;
}
memset(f,-1,sizeof(f));
for(int i=1;i<=m;i++)
f[0][i]=0;
for(int i=1;i<=k;i++)
for(int j=0;j<cnt[i];j++)
for(int k=m;k>=w[i][j];k--){
f[i][k]=MAX(f[i][k],f[i][k-w[i][j]]+v[i][j],f[i-1][k-w[i][j]]+v[i][j]);
}
if(f[k][m]==-1)
printf("Impossible\n");
else
printf("%d\n",f[k][m]);
}
return 0;
}