Codeforces 106C Buns 【0-1背包】

博客探讨了Codeforces 106C问题,涉及0-1背包的优化策略。通过使用背包算法,找到在限定面团和填充物数量下能获得最大价值的组合。提供了详细的题意解释、解决方案和AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 106C Buns

Lavrenty, a baker, is going to make several buns with stuffings and sell them.

Lavrenty has n grams of dough as well as m different stuffing types. The stuffing types are numerated from 1 to m. Lavrenty knows that he has ai grams left of the i-th stuffing. It takes exactly bi grams of stuffing i and ci grams of dough to cook a bun with the i-th stuffing. Such bun can be sold for di tugriks.

Also he can make buns without stuffings. Each of such buns requires c0 grams of dough and it can be sold for d0 tugriks. So Lavrenty can cook any number of buns with different stuffings or without it unless he runs out of dough and the stuffings. Lavrenty throws away all excess material left after baking.

Find the maximum number of tugriks Lavrenty can earn.

Input
The first line contains 4 integers n, m, c0 and d0 (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10, 1 ≤ c0, d0 ≤ 100). Each of the following m lines contains 4 integers. The i-th line contains numbers ai, bi, ci and di (1 ≤ ai, bi, ci, di ≤ 100).

Output
Print the only number — the maximum number of tugriks Lavrenty can earn.

Examples
input
10 2 2 1
7 3 2 100
12 3 1 10
output
241
input
100 1 25 50
15 5 20 10
output
200
Note
To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing.

In the second sample Lavrenty should cook 4 buns without stuffings.

题意:给你n克面团和m种填料,对于第i种填料,它剩余a[i]克,你可以选择b[i]克该填料和c[i]克面团来生产一件物品,价值为d[i]。你也可以直接使用c0克面团来生产一件物品,价值为d0。问你可以获得的最大价值。

思路:背包。
设置dp[i][j][k]为状态——在k克面团的前提下使用前i种填料且第i种填料生成j件物品的最大价值。

状态转移方程较复杂。
首先dp[i][][]的第一个状态取决于dp[i-1][][]的最后一个状态,去掉第一维。其次dp[][j+1][]仅仅取决于dp[][j][],那么我们在递推时,升序枚举j即可,这样去掉第二维。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
int dp[MAXN];
int main()
{
    int n, m, c0, d0; cin >> n >> m >> c0 >> d0;
    CLR(dp, 0);
    for(int i = c0; i <= n; i++) dp[i] = i / c0 * d0;
    for(int i = 1; i <= m; i++)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        for(int j = 1; j <= a / b; j++)
        {
            for(int k = n; k >= c; k--)
                dp[k] = max(dp[k-c] + d, dp[k]);
        }
    }
    cout << dp[n] << endl;
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值