zswx

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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  阅读(375)  评论(0编辑  收藏  举报