题意:给一个储钱罐,已知空的储钱罐和装了硬币的储钱罐的质量。然后给了n种硬币的质量和价值。
问储钱罐里最少有多少钱。
解法:完全背包。注意要初始化为 INF,要正好装满,如果结果是INF,输出This is impossible.
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
const int INF=0x3f3f3f3f;
int n,m,t;
int dp[],w[maxn],v[maxn];
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d%d",&a,&b);
int W=b-a;
scanf("%d",&n);
for(i=;i<n;i++) scanf("%d%d",&v[i],&w[i]);
for(i=;i<=W;i++) dp[i]=INF;
dp[]=;
for(i=;i<n;i++)
{
for(j=w[i];j<=W;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[W]==INF) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]);
}
}

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

  1. HDU 1114 Piggy-Bank(一维背包)

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

  2. hdu -1114(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 思路:求出存钱罐装全部装满情况下硬币的最小数量,即求出硬币的最小价值.转换为最小背包的问题. # ...

  3. HDU 1114 Piggy-Bank 完全背包 dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1114 完全背包的题目,要求输出最小价值.然后一定要把给出的背包重量全部用完. 就是问一个背包为k的大小,n件物品 ...

  4. --hdu 1114 Piggy-Bank(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 AC code: #include<bits/stdc++.h> using nam ...

  5. HDU 1114 Piggy-Bank 全然背包

    Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...

  6. HDU 1114 Piggy-Bank(完全背包模板题)

    完全背包模板题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...

  7. HDU 1114 【完全背包裸题】

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. HDU - 1114 Piggy-Bank 完全背包(背包恰好装满)

    Piggy-Bank Before ACM can do anything, a budget must be prepared and the necessary financial support ...

  9. HDU 1114 Piggy-Bank (完全背包)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. httpModules与httpHandlers之httpModules(转载)

    WapModule.cs:public class WapModule:IHttpModule{ public void Init(HttpApplication context)        {  ...

  2. WPF控件模拟双击事件

    Action a = () => { i += ; ) { Interval = }; timer.Elapsed += (sender, e) => { timer.Enabled = ...

  3. IE7下,input元素相对父级元素错位解决办法

    原因: 当input仅仅包含父元素,父元素拥有margin属性时,IE7的input就会错误的继承margin属性. 解决办法: 给input元素外面套一个span,label这样的内联元素,这样就会 ...

  4. 关于笔记本安装双系统windows and linux

    ps1.安装完成后,补充下如何设在win7为默认启动系统, 大家也都知道,在linux 运行当软件都是以配置文件来设置参数当,当然grub菜单也不例外, 修改菜单可以进入grub.conf [root ...

  5. 12个常用的js正则表达式

    在这篇文章里,我已经编写了12个超有用的正则表达式,这可是WEB开发人员的最爱哦. 1.在input框中只能输入金额,其实就是只能输入最多有两位小数的数字 //第一种在input输入框限制 <i ...

  6. getVisibleSize 和 getContentSize 和 getWinSize

    getVisibleSize:获得视口(可视区域)的大小,若是DesignResolutionSize跟屏幕尺寸一样大,则getVisibleSize便是getWinSize.getVisibleOr ...

  7. [ruby on rails] 跟我学之(5)显示所有数据

    之前的index页,显示的是hello world,现在将其修改为显示我们在rails console里面录入的数据. 1. 修改action 如之前的章节<[ruby on rails] 跟我 ...

  8. python代码中使用settings

    在具体的Django应用中,通过引入 django.conf.settings 使用配置,例: from django.conf import settings settings.configure( ...

  9. wget批量下载

    wget -i download.txt 这样就会把download.txt里面列出的每个URL都下载下来. wget -c http://the.url.of/incomplete/file 使用断 ...

  10. MYSQL索引失效的各种情形总结

    1) 没有查询条件,或者查询条件没有建立索引  2) 在查询条件上没有使用引导列  3) 查询的数量是大表的大部分,应该是30%以上.  4) 索引本身失效 5) 查询条件使用函数在索引列上,或者对索 ...