首页 > 代码库 > hdu1114Piggy-Bank(完全背包)

hdu1114Piggy-Bank(完全背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114

 1 #include <iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 int e,f;
 7 int v[10010];
 8 int w[10010];
 9 int dp[10010];
10 int main()
11 {
12    int t;
13    cin>>t;
14    while(t--)
15    {
16        int ww=100000;
17        int ans=0;
18        cin>>e>>f;
19        int val=f-e;
20        int n;
21        cin>>n;
22        for(int i=0;i<n;i++)
23         cin>>v[i]>>w[i];
24     for(int i=0;i<=val;i++) dp[i]=1000000;
25     dp[0]=0;
26     for(int i=1;i<=val;i++)
27     for(int j=0;j<n;j++)
28    {
29        if(i>=w[j])
30         dp[i]=min(dp[i-w[j]]+v[j],dp[i]);
31    }
32         if(dp[val]!=1000000)
33         printf("The minimum amount of money in the piggy-bank is  %d.\n",dp[val]);
34         else printf("This is impossible.\n");
35    }
36 }   

 

hdu1114Piggy-Bank(完全背包)