码迷,mamicode.com
首页 > 其他好文 > 详细

混合背包(多重背包+完全背包)—— POJ 3260

时间:2015-04-28 09:54:09      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

对应POJ题目:点击打开链接

The Fewest Coins
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5450   Accepted: 1653

Description

Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1C2 coins of valueV2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

Input

Line 1: Two space-separated integers: N and T
Line 2: N space-separated integers, respectively V1V2, ..., VN coins (V1, ...VN
Line 3: N space-separated integers, respectively C1C2, ..., CN

Output

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

Sample Input

3 70
5 25 50
5 2 1

Sample Output

3

Hint

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.

题意:给出n种面额的硬币以及每种面额的数量,买要T元的商品,给多了老板会找钱,老板也有n种面额的硬币,但没有数量限制,问在买T元的商品这个交易的过程中硬币交易的数量最少是多少(即是用来买商品的硬币数量跟老板找零的硬币数量的和最小是多少)


思路:买商品的钱有限,是多重背包问题,dp_buy[i]表示支付为i时需要的最少硬币数量;老板找零的硬币无限,是完全背包问题,dp_sell[i]表示找钱为i时需要的最少硬币数量;则min{dp_buy[i] + dp_sell[i-T]}即为所求。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define INF (1<<30)
#define MAXN 110
#define MAXT 24410
int dp_buy[MAXT]; //dp_buy[i]表示支付为i时需要的最少硬币数量
int dp_sell[MAXT]; //dp_sell[i]表示找钱为i时需要的最少硬币数量
int v[MAXN];
int c[MAXN];
int v_b[MAXN*15]; //二进制拆分后的硬币面值
int c_b[MAXN*15]; //二进制拆分后的硬币数量
int N, T;

int main()
{
	//freopen("in.txt", "r", stdin);
	int i, j, k, u, max_e, min_s, V;
	while(~scanf("%d%d", &N, &T))
	{
		max_e = 0; //最大的硬币面值
		u = 0;

		for(i = 1; i <= N; i++){
			scanf("%d", v + i);
			if(v[i] > max_e) max_e = v[i];
		}

		for(i = 1; i <= N; i++){
			scanf("%d", c + i);
			//二进制拆分
			j = 1;
			k = c[i];
			while(j <= k)
			{
				++u;
				v_b[u] = j * v[i];
				c_b[u] = j;
				k -= j;
				j <<= 1;
			}
			if(k > 0){
				++u;
				v_b[u] = k * v[i];
				c_b[u] = k;
			}
		}

		V = T + max_e * max_e; //付钱的最大金额

		for(i= 0; i <= V; i++) dp_sell[i] = dp_buy[i] = INF;
		dp_sell[0] = dp_buy[0] = 0;

		for(i = 1; i <= N; i++) //完全背包算出找零的最小数量
			for(j = v[i]; j <= V - T; j++)
				dp_sell[j] = MIN(dp_sell[j - v[i]] + 1, dp_sell[j]);

		for(i = 1; i <= u; i++) //多重背包(转化为01背包)算出付钱的最小数量
			for(j = V; j >= v_b[i]; j--)
				dp_buy[j] = MIN(dp_buy[j - v_b[i]] + c_b[i], dp_buy[j]);

		min_s = INF;

		for(i = T; i <= V; i++){
			if(dp_buy[i] == INF || dp_sell[i-T] == INF) continue;
			if(dp_buy[i] + dp_sell[i-T] < min_s)
				min_s = dp_buy[i] + dp_sell[i-T];
		}

		if(min_s != INF) printf("%d\n", min_s);
		else printf("-1\n");
	}
	return 0;
}






混合背包(多重背包+完全背包)—— POJ 3260

标签:

原文地址:http://blog.csdn.net/u013351484/article/details/45316921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!