Q: 既是多重背包, 还是找零问题, 怎么处理?

A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少

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 value V2, ...., 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.

题意:

John 去付款, 手上拿着各种面值的硬币, 每种面值的硬币有一个可以使用的上限, 店主支持找钱, 找钱所用的硬币是无限的

思路:

1. 证明背包的容量上限是个难点, 这里有个证明

http://www.cppblog.com/Davidlrzh/articles/135614.html

背包容量上限是 T + vmax^2

2. 使用倍增优化转化为 0/1 背包来做. 先求解 Jhon 的给钱方式, 再求解找钱方案, 两次 DP

3.  Jhon 的状态转移方程. dp[i][j] = min(dp[i][j], dp[i][j-w[i]]+1)

   店主的状态转移方程可以有两种表述, dp2[i][j] = min(dp2[i][j], dp2[i][j-w[i]]+1) (和 Jhon 的一样), 但是用到两个dp 数组

4. 第二种表述和 1252 相同, 仅使用一个dp 数组, 总的状态转移方程为 dp[j] = min([j-w[i]+1, dp[j+w[i]]+1);

总结:

1. 看别人的解题报告, 才知道店主找零的硬币是没有上限的, 这将题目的难度下降了很多, 难点就变成多重背包的优化了

2. 思路(3,4)的状态转移方程有误, 因为进行过倍增优化, 所以状态转移方程中并不是简单的+1, 而应该+倍增优化的倍数

3. 没判断输出为 INF 的情况, WA 了一次, 找到了测试用例地址

http://cerberus.delos.com:790/TESTDATA/DEC06_5.htm

代码:

#include <iostream>
using namespace std; int N,T;
int V[150], C[150];
int upload;
int dp[100000];
int stuff[10010];
int cnt[10010];
int len;
const int INF = 0X3F3F3F3F; /*
* 倍增优化转成 01 背包
* 处理 01 背包, 求解 Jhon 恰好付 v 元 所需要的硬币数
*/
void firstPass() {
len = 0;
for(int i = 0; i < N; i ++) {
int rem = C[i];
int j = 0; while(rem) { // 倍增优化转化成 01 背包
if(rem >= (1<<j)) {
stuff[len++] = V[i]*(1<<j);
cnt[len-1] = (1<<j);
rem -= (1<<j);
j++;
}else{
stuff[len++] = V[i]*rem;
cnt[len-1] = rem;
rem = 0;
}
}
} memset(dp, 0x3f, sizeof(int)*(upload+10));
dp[0] = 0;
for(int i = 0; i < len; i ++) {
for(int v = upload; v >= stuff[i]; v--) {
dp[v] = min(dp[v], dp[v-stuff[i]]+cnt[i]);
//printf("dp[%d], stuff[%d] = %d\n", v, i, stuff[i]);
}
}
} /*
* second pass 完全背包
* 状态转移方程, dp[v] = min(dp[v], dp[v+w[i]]+1)
* 要注意遍历顺序
*/
int secondPass() {
for(int i = 0; i < N; i ++) {
for(int v = upload-V[i]; v >= 0; v --) {
dp[v] = min(dp[v], dp[v+V[i]]+1);
}
}
return dp[T];
}
int main() {
freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin);
cin >> N >> T;
int vmax = 0;
int sum = 0;
for(int i = 0; i < N; i ++) {
scanf("%d", &V[i]);
vmax = max(vmax, V[i]);
}
upload = vmax*vmax + T; for(int i = 0; i < N; i ++) {
scanf("%d", &C[i]);
sum += C[i]*V[i];
}
if(sum < T)
cout << -1 << endl;
else {
firstPass();
int ans = secondPass();
if(ans != INF)
cout << secondPass() << endl;
else
cout << -1 << endl;
} return 0;
}

  

POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)的更多相关文章

  1. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  2. POJ 3260 The Fewest Coins(背包问题)

    [题目链接] http://poj.org/problem?id=3260 [题目大意] 给出你拥有的货币种类和每种的数量,商店拥有的货币数量是无限的, 问你买一个价值为m的物品,最少的货币流通数量为 ...

  3. POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)

    题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...

  4. poj 3260 The Fewest Coins

    // 转载自http://blog.163.com/benz_/blog/static/18684203020115721917109/算法不难看出,就是一个无限背包+多重背包.问题在于背包的范围.设 ...

  5. POJ 1252 Euro Efficiency(完全背包, 找零问题, 二次DP)

    Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...

  6. POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)

    题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少. 思路:老板会自动找钱,且按最少的找,硬 ...

  7. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  8. codevs 3961 硬币找零【完全背包DP/记忆化搜索】

    题目描述 Description 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从银行拿回最少的硬币数,并保证能用这些硬币发工资. 我们应该 ...

  9. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

随机推荐

  1. 【转】PNG文件结构

    本文转自:http://blog.csdn.net/bisword/article/details/2777121  前言 我们都知道,在进行J2ME的手机应用程序开发的时候,在图片的使用上,我们可以 ...

  2. TCP/IP协议分析

    一;前言 学习过TCP/IP协议的人多有一种感觉,这东西太抽象了,没有什么数据实例,看完不久就忘了.本文将介绍一种直观的学习方法,利用协议分析工具学习TCP/IP,在学习的过程中能直观的看到数据的具体 ...

  3. Redis学习之(一)

    Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的Web应用程序. Redis有三个主要特点,使它优越于其它键值数据存储系统 - Redis将其数据库完全保存在内存中, ...

  4. Java 并发编程学习笔记 理解CLH队列锁算法

    CLH算法实现 CLH队列中的结点QNode中含有一个locked字段,该字段若为true表示该线程需要获取锁,且不释放锁,为false表示线程释放了锁.结点之间是通过隐形的链表相连,之所以叫隐形的链 ...

  5. Linux下安装rpm出现error: Failed dependencies

    在Linux下安装rpm包时经常会遇到下面这个问题: error: Failed dependencies: xxxxxxxxxxxxxxxxxxxxxx 遇到此问题时可以在安装rpm包命令的后面加两 ...

  6. [JavaScript]JS调用PHP和PHP调用JS的方法举例

    http://blog.csdn.net/pleasecallmewhy/article/details/8592571 body { background: #C7EDCC !important; ...

  7. java.lang.OutOfMemoryError: Java heap space 解决方法

    从网上抄过来的,因为经常碰到这个问题,记录一下. java.lang.OutOfMemoryError: Java heap space 解决方法 这个问题的根源是jvm虚拟机的默认Heap大小是64 ...

  8. 关于c# 发射的调用并进行缓存

    private static object CreateObject(string AssemblyPath, string classNamespace) { object objObject = ...

  9. 二、Redis命令行和配置文件redis.windows.conf

    一.Redis发送命令的两种方式 redis-cli -h localhost -p 6379redis-cli ping 返回pong 证明正常 二.命令返回值 1.状态回复,如ping命令 2.错 ...

  10. golang中map并发读写问题及解决方法

    一.map并发读写问题 如果map由多协程同时读和写就会出现 fatal error:concurrent map read and map write的错误 如下代码很容易就出现map并发读写问题 ...