首页 > 其他 > 详细

Cash Machine(多重背包)

时间:2014-01-19 15:58:52      阅读:547      评论:0      收藏:0      [点我收藏+]

http://poj.org/problem?id=1276

bubuko.com,布布扣
 1 #include <stdio.h>
 2 #include <string.h>
 3 const int N=1000020;
 4 #define Max(a,b) (a)>(b)?(a):(b)
 5 int dp[N],cash;
 6 void ZeroOnePack(int cost)//01背包
 7 {
 8     for (int i = cash; i >= cost; i--)
 9     {
10         dp[i] = Max(dp[i],dp[i-cost]+cost);
11     }
12 }
13 void ComplexPack(int cost)//完全背包
14 {
15     for (int i = cost; i <= cash; i++)
16     {
17         dp[i] = Max(dp[i],dp[i-cost]+cost);
18     }
19 }
20 void MultiplePack(int cnt,int cost)//多重背包
21 {
22     if (cnt*cost > cash)
23         ComplexPack(cost);
24     else
25     {
26         int k = 1;
27         while(k < cnt)
28         {
29             ZeroOnePack(k*cost);
30             cnt-=k;
31             k<<=1;
32         }
33         ZeroOnePack(cnt*cost);
34     }
35 }
36 int main()
37 {
38     int n,cost[12],cnt[12];
39     while(~scanf("%d %d",&cash,&n))
40     {
41         for (int i = 1; i <= n; i++)
42         {
43             scanf("%d %d",&cnt[i],&cost[i]);
44         }
45         memset(dp,0,sizeof(dp));
46         for (int i = 1; i <= n; i++)
47         {
48             MultiplePack(cnt[i],cost[i]);
49         }
50         printf("%d\n",dp[cash]);
51     }
52     return 0;
53 }
View Code

Cash Machine(多重背包)

原文:http://www.cnblogs.com/lahblogs/p/3525908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!