Codeforces 106 C Buns【多重背包】

本文介绍了一个基于多重背包问题的烘焙场景应用案例。该案例通过计算有限原料条件下制作面包的最大经济价值,采用多重背包算法实现最优解。文章提供了一段完整的C++代码实现,展示了如何通过迭代优化来达到算法的最佳性能。

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

C. Buns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nmc0 and d0 (1 ≤ n ≤ 10001 ≤ m ≤ 101 ≤ c0, d0 ≤ 100). Each of the following m lines contains 4integers. The i-th line contains numbers aibici 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种填料,可以用c0克面粉做一个价值为d0的物品,接下来m行,每行四个元素,ai,bi,ci,di,表示有这种填料ai克,可以使用这种填料bi克和ci克面粉做一个价值为di的物品,问最大能够获得多少价值。


思路:


1、多重背包,设定n克面粉为背包容量,设定物品价值为背包价值。


2、剩下的就是裸的多重背包问题。


3、没尝试多重背包转0-1背包会不会超时,不过看起来应该不会超时。

Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int dp[1000000];
int v;
void zoreonepack(int val,int cost)
{
    for(int i=v;i>=cost;i--)
    {
        if(dp[i-cost]+val>dp[i])
        {
            dp[i]=dp[i-cost]+val;
        }
    }
}
void completepack(int val,int cost)
{
    for(int i=cost;i<=v;i++)
    {
        dp[i]=max(dp[i],dp[i-cost]+val);
    }
}
void multipack(int val,int cost,int num)
{
    if(num*cost>=v)
    {
        completepack(val,cost);
    }
    else
    {
        int k=1;
         while(k<num)
         {
             zoreonepack(k*val,k*cost);
             num-=k;k+=k;
         }
         zoreonepack(num*val,num*cost);
    }
}
int main()
{
    int n,m,c0,d0;
    while(~scanf("%d%d%d%d",&n,&m,&c0,&d0))
    {
        v=n;
        memset(dp,0,sizeof(dp));
        multipack(d0,c0,n/c0);
        for(int i=0;i<m;i++)
        {
            int ai,bi,ci,di;
            scanf("%d%d%d%d",&ai,&bi,&ci,&di);
            multipack(di,ci,ai/bi);
        }
        printf("%d\n",dp[v]);
    }
}
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值