1.题目来源HDU1114

Sample Input
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample Output
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

2.题目分析

题目首先给定一个空存钱罐的重量和这个存钱罐最多能装进去的重量,现在需要在不打破这个存钱罐的情况下猜测里面最少的钱每种钱的数量不做限制,条件是必须装满,同时给出每种钱币的价值和重量。
参考背包九讲,这属于完全背包问题,初始状态为装满的情况,所以放钱币的策略就变成了放几个问题,有的钱币组合不合理就设置得比较大。

3.代码如下

#include<iostream>
#include<algorithm>
using namespace std;
const int num = 10005;
const int abc = 9999999999;
int main(void)
{
//(1 <= P <= 50000, 1 <= W <= 10000)
//1 <= E <= F <= 10000, N (1 <= N <= 500)(coins)
int dp[num], C[num], W[num], T, N, E, F, V, i, j;
cin >> T;
while (T-- > 0) {
cin >> E >> F; // the weight of an empty pig and of the pig filled with coins
cin >> N; //the number of various coins used in the given currency.
V = F - E; //the rest of the space
//C is the value of the coin in monetary units, W is it's weight in grams.
for (i = 1; i <= N; i++)
cin >> W[i] >> C[i];
dp[0] = 0;
for (i = 1; i <= V; i++) dp[i] = abc;
for (i = 1; i <= N; i++) {
for (j = C[i]; j <= V; j++) {
dp[j] = min(dp[j], dp[j - C[i]] + W[i]);
}
}
if (dp[V] == abc) cout << "This is impossible."<<endl;
else cout << "The minimum amount of money in the piggy-bank is "<<dp[V]<<"."<<endl;
}
return 0;
}

动态规划(二)HDU1114的更多相关文章

  1. 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表

    [题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...

  2. 【洛谷】【动态规划(二维)】P1508 Likecloud-吃、吃、吃

    [题目描述:] 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏眼花之时,眼前突然闪现出了一个n*m(n and m<=200)的矩型的巨 ...

  3. 【动态规划/二维背包问题】mr355-三角形牧场

    应该也是USACO的题目?同样没有找到具体出处. [题目大意] 和所有人一样,奶牛喜欢变化.它们正在设想新造型牧场.奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场.她拥有N(3≤N≤40)块木板, ...

  4. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  5. 动态规划(二维背包问题):UVAoj 473

     Raucous Rockers  You just inherited the rights to n previously unreleased songs recorded by the pop ...

  6. 【洛谷】【动态规划/二维背包】P1855 榨取kkksc03

    [题目描述:] ... (宣传luogu2的内容被自动省略) 洛谷的运营组决定,如果...,那么他可以浪费掉kkksc03的一些时间的同时消耗掉kkksc03的一些金钱以满足自己的一个愿望. Kkks ...

  7. 动态规划二:最长公共子序列(LCS)

    1.两个子序列:X={x1,x2....xm},Y={y1,y2....yn},设Z={z1,z2...zk}. 2.最优子结构: 1)如果xm=yn ,则zk=xm=yn且Zk-1是Xm-1和Yn- ...

  8. kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)

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

  9. 【学习笔记】动态规划—各种 DP 优化

    [学习笔记]动态规划-各种 DP 优化 [大前言] 个人认为贪心,\(dp\) 是最难的,每次遇到题完全不知道该怎么办,看了题解后又瞬间恍然大悟(TAT).这篇文章也是花了我差不多一个月时间才全部完成 ...

随机推荐

  1. linux shell内置判断

    内置判断,成功的时候返回0,不成功返回非零 test  判断表达式 [ 判断表达式 ]       注意前后必须留空格哦 数值运算 -eq   等于 -ne   不等于 -gt     大于 -ge ...

  2. Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题

    1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...

  3. 云端基于Docker的微服务与持续交付实践

    云端基于Docker的微服务与持续交付实践笔记,是基于易立老师在阿里巴巴首届在线技术峰会上<云端基于Docker的微服务与持续交付实践>总结而出的. 本次主要讲了什么? Docker Sw ...

  4. PHP腾讯与百度坐标转换

    function coordinate_switch($a,$b){//百度转腾讯坐标转换 $a = Latitude , $b = Longitude $x = (double)$b - 0.006 ...

  5. Kubernetes JSONpath Support

    export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=test-nginx,ap ...

  6. SQA计划和测试规程

    一.SQA计划 (一)目的 本计划的目的是定义我们该小组所做的“云医院”项目的SQA任务和职责,在项目过程中应遵循的流程.规范和约定等,以确保软件质量得到维持. (二)范围 本计划应用于“云医院”项目 ...

  7. MovieReview—The Foreigner (英伦对决)

    The Foreigner's theme is revenge.The whole story is carried out in two dimensions:political struggle ...

  8. Computer Hardware

    Computer Hardware Para 1 Computer hardware can be divides into four categories: input hardware, stor ...

  9. iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant

    /* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * mul ...

  10. HDU 5675 智慧数

    题意:给一个n ,能否找到正整数x,y使得 x^2 -y^2 = n: 分析:打表可以找出规律,3,5,7,9....,8,12,16,20..... 和奇数,4的倍数有关. 数学证明: