题目链接: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;
}

Codeforces 106C Buns 【0-1背包】相关推荐

  1. Python 0/1背包、动态规划

    参考:http://www.cnblogs.com/fcyworld/p/6243012.html Python 0/1背包.动态规划 0/1背包问题:在能承受一定重量的背包中,放入重量不同,价值不同 ...

  2. P1417 烹调方案 (0/1背包+贪心)

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  3. 算法分析与设计——蛮力法0/1背包

    蛮力法0/1背包 蛮力法 蛮力法是一种简单直接解决问题的方法,常常直接基于问题的描述,所以蛮力法也是最容易应用的方法. 蛮力法所依赖 的基本技术是遍历,即采用一定的策略依次处理待求解问题的所有元素,从 ...

  4. 数据结构与算法 / 回溯算法(八皇后、0 - 1 背包)

    回溯算法,顾名思义,就是在没有得到最优解的前提下,不断的返回至前面的岔路口位置,重新选择,直至遍历了所有的情况或者得到的预期最优解的情况下再结束. 与贪心算法不同的是,回溯算法理论上是可以得到最优解, ...

  5. 【例1】 0/1背包《信息学奥赛一本通》【解法一】 02

    /* [例1] 0/1背包<信息学奥赛一本通>[解法一] 02 http://ybt.ssoier.cn:8088/problem_show.php?pid=1267 */ #includ ...

  6. HDOJ 2602-Bone Collector(0/1背包模板、打印方案及滚动数组解法)

    0/1背包 一.Bone Collector 解法一:二维数组解法(0/1背包模板代码) 1.1 0/1背包打印方案代码 解法二:滚动数组(一维)解法 2.1 一维滚动数组例题 E-爱玩游戏的Tom ...

  7. 动态规划(五)——0/1背包

    0/1背包 一.0/1背包问题 1.实例讲解 2.DP求解0/1背包 3.输出0/1背包方案 二.0/1背包题目代码(持续更新) 一.0/1背包问题 给定n种物品和一个背包,物品i的重量为wi,价值为 ...

  8. CodeForces - 1514B AND 0, Sum Big【快速模幂】

    B. AND 0, Sum Big time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  9. HDU1248 寒冰王座【0/1背包+DP】

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  10. 动态规划算法初步(6)——0/1 背包

    动态规划算法初步(6) 例题五:0/1 背包(背包型) 题目: 一个旅行者有一个最多能装m公斤物品的背包,现在有n件物品,它们的重量分别是w1,w2,-,wn,它们的价值分别为c1,c2,-,cn.若 ...

最新文章

  1. 宝塔Linux常用命令
  2. 可能是最好的跨域解决方案了
  3. IE下判断IE版本的语句
  4. Touch Event
  5. 三星s10android10功能,三星S10系列现场上手体验:“安卓机皇”真的名副其实
  6. 增加堆大小–谨防眼镜蛇效应
  7. 零基础学UI设计,正确的学习方法讲解!
  8. 网络安全系列之五十四 为GRUB引导菜单设置密码
  9. iOS获取iPhone系统等信息和服务器返回空的异常处理
  10. ko.js循环绑定值问题(工作遇见)
  11. mysql插件的初始化
  12. 盘点一道使用Python编程来实现高斯计算的基础算术题目
  13. 彪悍的人生不需要解释!
  14. 如何为firefox安装视频播放器
  15. Excel根据名字批量插入图片
  16. 大数据和云计算技术周报(第81期)
  17. 33MW太阳能厂动工 日本福岛开启复兴之路
  18. Vuforia的学习(一)---Vuforia的介绍
  19. nanopi emmc定制系统量产步骤
  20. 2021 蓝桥杯省赛第一场 C++ 大学 B 组

热门文章

  1. 刚体运动学公式_理论力学:运动学
  2. VMware 虚拟机系统 与 win10 共享文件夹问题的解决
  3. 【异常处理】The CXX compiler identification is unknown
  4. Java IO与文件读写
  5. JAVA计算机毕业设计的问卷调查系统设计与实现源码+数据库+系统+lw文档
  6. 日期计算器输入天数计算日期_计算日期范围内的活动
  7. 双系统windows+linux如何正确删除linux
  8. 继续教育-职场学习法 试题及答案
  9. [洛谷P3939]数颜色
  10. 网红茶饮难逃“短命”之殇,喜茶能否打破这个魔咒?