Codeforces Beta Round #82 (Div. 2)第三题:简单的动态规划——多重背包

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 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.

Sample test(s)
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.

刚开始想贪心,但是很快就被否决了,应该是动态规划,后来仔细一想便发现这是一道典型动态规划裸题,就是多重背包,其实多重背包以前也做过一道类似的题,但是忘了,所以只能饮恨至此。

之后又重新看了下背包九讲中的多重背包,原来代码量也不长,关键要靠自己的理解,理解深刻了怎么会忘呢?惭愧啊!

言归正传,这题中将dough看做背包的容量,将ai/bi作为每种物品提供的最多数量,将不用stuffing的也作为一种物品,则此题便成为一道多重背包问题,动态规划状态转移方程为:

   dp[i+1][j]  = max{dp[i][j-k*c[i+1]]+k*d[i+1]};(0<=k<=num[i])

当然,也可以将维度从二维压缩到一维,这要看自己的熟练程度了,要特别注意dp数组的初始化,那是有讲究,有着一定的含义的,请给予高度重视。好了不多说了,上代码:

  1. #include<cstdio> 
  2.  
  3. int num[12],c[12],d[12]; 
  4. int dp[12][1010]; 
  5.  
  6. int main() 
  7. { 
  8.     int n,m,c0,d0; 
  9.     scanf("%d%d%d%d",&n,&m,&c0,&d0); 
  10.     num[0]=1005;  c[0]=c0;    d[0]=d0; 
  11.     int i,j,k,a,b; 
  12.     for(i=1;i<=m;i++) 
  13.     { 
  14.         scanf("%d%d",&a,&b); 
  15.         num[i] = a/b; 
  16.         scanf("%d%d",&c[i],&d[i]); 
  17.     } 
  18.     for (i = 0; i <= n; i++) 
  19.         dp[0][i] = i / c0 * d0; 
  20.  
  21.     for(i=0;i<m;i++) 
  22.     { 
  23.         for(j=n;j>=0;j--) 
  24.         { 
  25.             for(int k=0;k<=num[i+1];k++) 
  26.             { 
  27.                 if(j - k*c[i+1] < 0)  break; 
  28.                 if(dp[i+1][j] < dp[i][j-k*c[i+1]]+k*d[i+1]) 
  29.                 { 
  30.                     dp[i+1][j] = dp[i][j-k*c[i+1]]+k*d[i+1]; 
  31.                 } 
  32.             } 
  33.         } 
  34.     } 
  35.     int res = 0; 
  36.     for(i=0;i<=n;i++) 
  37.     { 
  38.         if(dp[m][i]> res) 
  39.             res = dp[m][i]; 
  40.     } 
  41.     printf("%d\n",res); 
  42.  
  43.     return 0; 
  44. }
posted on 2011-08-20 12:18 zswx 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zswx/archive/2011/08/20/2147070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值