POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的。

怎么起个这么intimidating(吓人)的名字?

其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了。

本题要求装满的,故此增加个限制就可以了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
inline int min(int a, int b) { return a < b? a : b; }

const int MAX_W = 10001;
const int MAX_N = 501;
int val[MAX_N] = {0};
int wei[MAX_N] = {0};
int tbl[MAX_W];

int bagDP(int N, int W)
{
	for (int i = 0; i <= W; i++) tbl[i] = 0;

	for (int j = wei[1]; j <= W; j += wei[1])
		tbl[j] = val[1] + tbl[j-wei[1]];

	for (int i = 2; i <= N; i++)
	{
		for (int j = wei[i]; j <= W; j++)
		{
			if (j-wei[i]==0 || tbl[j-wei[i]])
			{
				if (tbl[j]) tbl[j] = min(tbl[j],tbl[j-wei[i]]+val[i]);
				else tbl[j] = tbl[j-wei[i]]+val[i];
			}
		}
	}
	return tbl[W];
}

int main()
{
	int E, F;//weight of an empty pig and of the pig filled with coins
	int T, N;// P:value, W: weight
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d %d %d", &E, &F, &N);
		for (int i = 1; i <= N; i++)
		{
			scanf("%d %d", &val[i], &wei[i]);
		}
		int ans = bagDP(N, F-E);
		if (ans) printf("The minimum amount of money in the piggy-bank is %d.\n", ans);
		else puts("This is impossible.");
	}
	return 0;
}

POJ 1384 Piggy-Bank 背包DP,布布扣,bubuko.com

时间: 2024-08-07 17:00:29

POJ 1384 Piggy-Bank 背包DP的相关文章

POJ 1384 Piggy-Bank(完全背包)

http://poj.org/problem?id=1384 题意: 现在有n种硬币,每种硬币有特定的重量cost[i] 克和它对应的价值val[i]. 每种硬币可以无限使用. 已知现在一个储蓄罐中所有硬币的总重量正好为m克, 问你这个储蓄罐中最少有多少价值的硬币? 如果不可能存在m克的情况, 那么就输出" This is impossible.". 分析: 由于每种硬币可以无限使用, 所以是明显的完全背包问题. 本题的限制条件: 硬币总重量正好等于m. 本题的目标条件: 硬币总价值尽

poj 3624 Charm Bracelet 背包DP

Charm Bracelet Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (

poj 2229 【完全背包dp】【递推dp】

poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 8281 Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an

POJ 1384 Piggy-Bank 完全背包分析

给定储蓄罐空的和满的重量,有n种硬币,硬币有价值和重量,给出各种硬币的价值p[i]和对应的重量w[i],求储蓄罐里面硬币的最小价值,如果没有符合要求的放硬币的方式,输出 “this is impossible”. 思路: 相当于完全背包求最小值,n中硬币对应n个物体,物体可以取无限次,存储罐里硬币重量(满罐减空罐)相当于背包的体积V. 法一: 直接扩展01背包的方程,用dp[i,v]表示取前i种硬币,存储罐重量最大为v时的最小价值.状态转移方程为:dp[i,v]=min(dp[i-1,v-k*w

POJ 1384(完全背包)

题目大意:给出罐子的初始重量和装满钱币后的重量,然后给出几组钱币的类型(币值和重量),问装满这个罐子后最少可以放多少钱. 思路:主要是注意初始化,然后写出状态方程,dp[v]=min(dp[v],dp[v-w[j]]+c[j]); 代码如下: #include <iostream>#include <stdio.h>#include <memory.h>#include <math.h>#define MAX 1000000using namespace s

poj 1384 Piggy-Bank(完全背包)

http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7900 Accepted: 3813 Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income f

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

题目描述 Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值k求最少要用多少个硬币. 输入 第一行一个数 n, 1 <= n <= 200. 接下来一行 n 个整数b1, b2,..., bn, 1 <= b1 < b2 < ... < b n <= 20 000, 第三行 n 个整数c1, c2,..., cn, 1 <

poj 2063 基础完全背包

这是一题基础的完全背包,适合初学者来理解完全背包 题意:有 n 种债券可以买 ,  每种债券的价格为 w , 每一年的收益为 p , 给你 wi 块钱 , 和 years 年的时间 , 我们最大的收益是是多少? 因为 , 每种债券可以买任意多个 , 所以这是一个简单的完全背包,但是由于基数(体积)太大 , 所以需要优化一下 : 由题意我们知道 , 每种债券的价格都是 1000 的倍数 , 那么我们就让每种债券的价格都 除以 1000 , 并且把 p 也除以 1000 , 这样就MTE , 也不会

POJ 1742 Coins (多重背包)

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some