poj1276--Cash Machine--多重背包

Description

假设你现在有现金cash,有n种商品,给定你它们各自的价格和数量。如果你想尽可能多地购买商品,请求出最多能买商品的总价值。

Sample Input

735 3 4 125 6 5 3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10

Sample Output

735

630

0

0

题解:

  最近真的在水题耶ww(居然好意思说)

  莫名其妙因为cash==0||n==0的情况WA了两次……其实不写就好了qwq

  就是一个多重背包的裸题吧,dp[j]表示当前容量为j时,最多能花出去的钱数。sum[j]表示的是当前物品购买的数量。

 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cstring>
 6 using namespace std;
 7 const int maxn=100009;
 8 int f[maxn],g[maxn];
 9 int cash,n,w[maxn],num[maxn],sum[maxn],ans=0;
10 int main()
11 {
12     while(scanf("%d%d",&cash,&n)!=EOF)
13     {
14         for(int i=1;i<=n;i++)
15             scanf("%d%d",&num[i],&w[i]);
16         f[0]=0;
17         memset(f,0,sizeof(f));
18         for(int i=1;i<=n;i++)
19         {
20             memset(sum,0,sizeof(sum));
21             for(int j=w[i];j<=cash;j++)
22             {
23                 if(f[j]<f[j-w[i]]+w[i]&&sum[j-w[i]]+1<=num[i])
24                 {
25                     sum[j]=sum[j-w[i]]+1;
26                     f[j]=f[j-w[i]]+w[i];
27                 }
28             }
29         }
30         printf("%d\n",f[cash]);
31     }
32     return 0;
33 }
View Code

 

posted @ 2017-09-01 07:29  BK-Edwina  阅读(129)  评论(0编辑  收藏  举报