Esempio n. 1
0
int knp(int wo,int wn,int wt[][2],int val[],int n){
	if(dp[wo][wn][n]!=-1) return dp[wo][wn][n]; 
	if(n==0 || (wo<=0 && wn<=0)) dp[wo][wn][n]=0;
	if(wo<wt[n-1][0] && wn<wt[n-1][1]) dp[wo][wn][n]=knp(wo,wn,wt,val,n-1);
	else dp[wo][wn][n]=max(val[n-1]+knp(wo-wt[n-1][0],wn-wt[n-1][1],wt,val,n-1),knp(wo,wn,wt,val,n-1));
	return dp[wo][wn][n];
}
Esempio n. 2
0
TEST_F(KnapsackProblemTest, Evaluate1) {
    auto items = exampleItemsLarge();
    KnapsackProblem knp(items.begin(), items.end(), 3);
    Knapsack k;
    k.add(items[0].second);
    EXPECT_EQ(100, knp.evaluate(k));
}
Esempio n. 3
0
int main(int argc, char const *argv[])
{
	int t;
	scanf("%d",&t);
	while(t--){
		int wo,wn,wt[1001][2],val[1001];
		int n;
		scanf("%d %d",&wo,&wn);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d",&wt[i][0],&wt[i][1]);
			scanf("%d",&val[i]);
		}
		int res=knp(wo,wn,wt,val,n);
		printf("%d\n",res);
	}
	return 0;
}
Esempio n. 4
0
TEST_F(KnapsackProblemTest, ConvertKnapsack) {

    vector<ItemPtr> items;

    auto i1 = std::make_shared<Item>(7,2);
    auto i2 = std::make_shared<Item>(7,2);

    items.push_back(i1);
    items.push_back(i2);

    KnapsackProblem knp(items, 10);

    auto result = knp.convertVectorToKnapsack(vector<int> {1,0});

    EXPECT_TRUE(result.contains(i1));
    EXPECT_FALSE(result.contains(i2));

}