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

思路分析:该问题要求为多重背包问题,使用多重背包的解法即可;假设dp[v]表示容量为v的背包中能够装下的最少的价值,因为一件物品可以装无限数次,所以可以得到递推公式: dp[v] = Min(dp[v], dp[v- c[i]] + w[i]);

代码如下:

import java.util.*;

public class Main {
static final int MAX_N = 10000 + 100;
static final int MAX_INT = 100000000;
static int[] w = new int[MAX_N];
static int[] c = new int[MAX_N];
static int[] dp = new int[MAX_N]; public static int Min(int a, int b) {
return a < b ? a : b;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int case_times = in.nextInt(); while (case_times-- != 0) {
int v_pig, v_total, V, N; v_pig = in.nextInt();
v_total = in.nextInt();
N = in.nextInt();
V = v_total - v_pig;
Arrays.fill(dp, MAX_INT);
dp[0] = 0;
for (int i = 1; i <= N; ++ i){
w[i] = in.nextInt();
c[i] = in.nextInt();
}
for (int i = 1; i <= N; ++ i)
for (int v = c[i]; v <= V; ++ v)
dp[v] = Min(dp[v], dp[v - c[i]] + w[i]);
if (dp[V] == MAX_INT)
System.out.println("This is impossible.");
else
System.out.println("The minimum amount of money in the piggy-bank is " + dp[V] + ".");
}
}
}

hdoj 1114 Piggy-Bank(完全背包+dp)的更多相关文章

  1. hdoj 5119 Happy Matt Friends 背包DP

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...

  2. HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)

    HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...

  3. 【bzoj1531】[POI2005]Bank notes 多重背包dp

    题目描述 Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值 ...

  4. 背包DP HDOJ 5410 CRB and His Birthday

    题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == ...

  5. Kattis - bank 【简单DP】

    Kattis - bank [简单DP] Description Oliver is a manager of a bank near KTH and wants to close soon. The ...

  6. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  7. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  8. HDU 5501 The Highest Mark 背包dp

    The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  9. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  10. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

随机推荐

  1. Android ProgressBar实现加载进度条

    progressBar Android进度条组件.   progressBar的关键属性:      android:max="100"     最大显示进度条      andr ...

  2. sf

    #include <stdio.h> #include <time.h> #include <stdlib.h> #define MAXN 150 //最大节点数 ...

  3. C语言 格式说明符

    整数 lL代表long %#0 和%#x 可打印出八进制和十六进制前缀 short int long long long 无符号 八进制 %ho %o %Lo %LLo 十进制 %hu %u %Lu ...

  4. Core开发-MVC 使用dotnet 命令创建Controller和View

    NET Core开发-MVC 使用dotnet 命令创建Controller和View   使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使 ...

  5. Esxi主机虚拟机迁移注意事项

    1. Esxi主机上的虚拟机迁移只能是低----->高,或版本一样的才能进行迁移 [如Esxi5.1---->Esxi5.5]ok, 而Esxi5.5----->Esxi5.1 no ...

  6. SQL Server 查看数据页面

    第一步: 找到表的第一页dbcc ind(db_name,table_name,-1); 例子. dbcc ind(studio,person,-1);# pageFID  是文件号   pagePI ...

  7. oracle字符集查看修改

    一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库 ...

  8. SICP 练习 (2.9)解决摘要:宽度和区间运算的关系间隔

    SICP 2.9 像是一个数学题,要我们证明区间的和与差的宽度是被加和被减的区间的宽度的函数,而对于乘法和除法来说不成立. 书中所谓宽度就是区间起点和终点差的一半.以我看来更像是区间宽度的一半.无论怎 ...

  9. android 强制设置横屏 判断是横屏还是竖屏

    判断activity 是横屏还是竖屏  方法 1: //根据设备配置信息 Configuration cf= this.getResources().getConfiguration(); //获取设 ...

  10. 加入功能区buttonRibbon Button到SP2010特定列表或库

    加入功能区button到SP2010某一列表或库         有时候你须要给列表/库的功能区加入新button--没有什么比这更简单的了. 你仅仅须要新建一个SP项目.加入一个feature,加入 ...