hdu 1114 基础完全背包

题意:给一个储钱罐,已知空的储钱罐和装了硬币的储钱罐的质量。然后给了n种硬币的质量和价值。

问储钱罐里最少有多少钱。

解法:完全背包。注意要初始化为 INF,要正好装满,如果结果是INF,输出This is impossible.

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=550;
 9 const int INF=0x3f3f3f3f;
10 int n,m,t;
11 int dp[100100],w[maxn],v[maxn];
12 int main()
13 {
14     int i,j,k;
15     #ifndef ONLINE_JUDGE
16     freopen("1.in","r",stdin);
17     #endif
18     scanf("%d",&t);
19     while(t--)
20     {
21         int a,b;
22         scanf("%d%d",&a,&b);
23         int W=b-a;
24         scanf("%d",&n);
25         for(i=0;i<n;i++)   scanf("%d%d",&v[i],&w[i]);
26         for(i=1;i<=W;i++) dp[i]=INF;
27         dp[0]=0;
28         for(i=0;i<n;i++)
29         {
30             for(j=w[i];j<=W;j++)
31             {
32                     dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
33             }
34         }
35         if(dp[W]==INF)  printf("This is impossible.\n");
36         else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]);
37     }
38 }
时间: 2024-10-12 09:11:04

hdu 1114 基础完全背包的相关文章

HDU 1114 Piggy-Bank(完全背包 DP)

题意  知道空存钱罐的重量和装满钱的存钱罐的重量及每种币值的重量   求存钱罐里至少有多少钱 裸的完全背包  但是是求最小值  所以初始0要变成初始INF  max也要变成min #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 10005, INF = 0x3f3f3f3f; int val[N], wei[N], d[N]; int ma

F - Piggy-Bank HDU 1114 (完全背包的变形+初始化细节)

F - Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1114 Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for th

HDU 1114 Piggy-Bank(一维背包)

题目地址:HDU 1114 把dp[0]初始化为0,其它的初始化为INF.这样就能保证最后的结果一定是满的,即一定是从0慢慢的加上来的. 代码例如以下: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #incl

hdu 1114 Piggy-Bank 完全背包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意分析:给出存钱罐存钱前后的重量,以及钱的种类及其价值和种类, 要求装满存钱罐最小的价值.  完全背包 /*Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13578 Accepted Submission(s):

HDU 1114 Piggy-Bank 全然背包

Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action come

Jam&#39;s balance HDU - 5616 (01背包基础题)

Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side are the same weight. Weights can be put on left side or right side arbitrarily. Please tell whether the balance can measure an object of weight M. In

[HDU 1114] Piggy-Bank (动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 简单完全背包,不多说. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <iterator> 7 #include <vector> 8 using

hdu 1114 完全背包问题

题意:给定背包体积与物品的体积与价值 求正好放完的最小价值#include<iostream> using namespace std; int min(int a,int b) { if(a<b) return a; return b; } int main() { int t,m1,m2,n,i,j; int v[502],w[502],dp[10005],m; cin>>t; while(t--) { cin>>m1>>m2; m=m2-m1;

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>