POJ3260---The Fewest Coins(混合背包)
来源:互联网 发布:网络政治参与方式 编辑:程序博客网 时间:2024/05/16 17:21
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 hasN (1 ≤ N ≤ 100) different coins, with values V1,V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carryingC1 coins of value V1, C2 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 2: N space-separated integers, respectively V1, V2, ...,VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ...,CN
Output
Sample Input
3 705 25 505 2 1
Sample Output
3
Hint
混合背包问题,主要是求上界比较蛋疼。
#include <cstdio>#include <algorithm>using namespace std;const int maxn = 105;const int inf = 1 << 18;int v[maxn], c[maxn];int dp1[20005], dp2[20005];int n, m;void complete_pack(int *dp, int cost, int weight, int limit){ for (int i = cost; i <= limit; i++) dp[i] = min(dp[i], dp[i - cost] + weight);}void zero_one_pack(int *dp, int cost, int weight, int limit){ for (int i = limit; i >= cost; i--) dp[i] = min(dp[i], dp[i - cost] + weight);}void multi_pack(int *dp, int cost, int weight, int limit, int amount){ if (cost * amount >= limit) { complete_pack(dp, cost, weight, limit); return; } int k = 1; while (k < amount) { zero_one_pack(dp, k * cost, k * weight, limit); amount -= k; k <<= 1; } zero_one_pack(dp, amount * cost, amount * weight, limit);}int main(){ while (scanf("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", &v[i]); for (int i = 1; i <= n; i++) scanf("%d", &c[i]); for (int i = 0; i <= m + 10000; i++) dp1[i] = dp2[i] = inf; dp1[0] = dp2[0] = 0; for (int i = 1; i <= n; i++) { complete_pack(dp2, v[i], 1, m + 10000); //找的钱是完全背包 multi_pack(dp1, v[i], 1, m + 10000, c[i]); //付的钱是多重背包 } int ans = inf; for (int i = m; i <= m + 10000; i++) { if (dp1[i] + dp2[i - m] < ans) ans = dp1[i] + dp2[i - m]; } printf("%d\n", ans == inf ? -1 : ans); } return 0;}
- POJ3260:The Fewest Coins(混合背包)
- POJ3260---The Fewest Coins(混合背包)
- POJ3260--The Fewest Coins(混合背包)
- poj3260 The Fewest Coins
- poj3260 The Fewest Coins
- 【POJ3260】【The Fewest Coins】
- 【POJ3260】【The Fewest Coins】
- POJ3260 The Fewest Coins
- POJ3260 The Fewest Coins 多重背包+完全背包
- 【POJ3260】The Fewest Coins 多重背包+完全背包
- POJ 3260 The Fewest Coins / 混合背包
- POJ_3260 The Fewest Coins(混合背包)
- 【DP|混合背包】POJ-3260 The Fewest Coins
- poj3260[USACO2006,Dec]The Fewest Coins最少找零
- POJ 3260 The Fewest Coins (混合背包--多重背包+完全背包)
- POJ 3260 The Fewest Coins(混合背包+鸽巢原理)
- poj 3260 The Fewest Coins 混合背包+上限处理 ★★★
- poj 3260 The Fewest Coins(背包)
- Axure RP 7.0注册码
- 黑马程序员————Foundation框架_知识总结(下)
- 本地生活服务api
- 别再自称“码农”了,编程是门思维的艺术
- POJ3281
- POJ3260---The Fewest Coins(混合背包)
- Java mongodb System.js 应用
- 网页排名PR值及其他——搜索引擎排序的科学(启)
- 上传图片中文乱码
- 【tcp-ip学习笔记】tcp-ip协议
- (1.5.2.7)最大公约数
- 使用jquery获取url以及使用jquery获取url参数以及中文编码乱码的问题
- LeetCode算法题之Next Permutation
- 将 text 文件里的数据导入到 mysql 数据库中