Hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

大意不再赘述。

思路:多重背包。

数组的大小根据题目的要求定,一般num数组是有少种物品开多大,V与W同样, f的大小是根据最大的cost或者weight * num[i]来定的,小于最大背包容量就行。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

const int INF = 0x3f3f3f3f;

int f[110];
int V[110], W[110];
int num[110];

int n, C;

void init()
{
	memset(f, 0, sizeof(f));
}

void Zp(int cost, int weight)
{
	for(int v = C; v >= cost; v--)
	{
		f[v] = max(f[v], f[v-cost]+weight);
	}
}

void Cp(int cost, int weight)
{
	for(int v = cost; v <= C; v++)
	{
		f[v] = max(f[v], f[v-cost]+weight);
	}
}

void Mp(int cost, int weight, int amount)
{
	if(cost * amount >= C)
	{
		Cp(cost, weight);
		return ;
	}
	else
	{
		int k = 1;
		while(k < amount)
		{
			Zp(k*cost, k*weight);
			amount -= k;
			k *= 2;
		}
		Zp(cost*amount, weight*amount);
	}
}

int dp()
{
	for(int i = 1; i <= n; i++)
	{
		Mp(V[i], W[i], num[i]);
	}
	return f[C];
}

void read_case()
{
	init();
	scanf("%d%d", &C, &n);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d%d%d", &V[i], &W[i], &num[i]);
	}
}

void solve()
{
	read_case();
	int ans = dp();
	printf("%d\n", ans);
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值