POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins
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 V1, V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1, C2 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 V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., 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
题目大意:
FJ同学去买东西,东西的价值为T,他和卖家都有N种金币,FJ希望交易完成时金币变化最小。
求最少的金币变化数量。FJ的金币个数有限,奸商的金币数目无限。
解题思路:
背包问题,FJ的每种金币个数有限可以看做是多重背包问题,奸商的金币数目无限可以看做是完全背包问题。
设F1[i]为FJ付款为i时的最小金币数,设F2[i]为奸商找钱为i时的最小金币数。
则F1[i+T]+F2[i]就是所求的最小金币变化数量。(F1用多重背包求解,F2用完全背包求解)
PS:这里的背包求得是最小价值,且要恰好装满。故初始化数组时应 F[0]=0,F[1-MAXN]=INT_MAX;(好久没做背包了,下意识把F[1]=0了,结果T==1时总是输出0,查了好久。。。)
Code:
#include<string>
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<limits.h>
#define MAXN 1000000
#define INF 9999999 //背包被调 直接抄的背包九讲,因为有两个数组,增加一个数组参数
using namespace std;
int N,V,c[MAXN+],a[MAXN+],w=,f1[MAXN+],f2[MAXN+];
int min(int a,int b)
{
return a>b?b:a;
}
void ZeroOnePack(int cost,int weight,int f[]) //01背包
{
for (int v=V; v>=cost; v--)
f[v]=min(f[v],f[v-cost]+weight);
}
void CompletePack(int cost,int weight,int f[]) //完全背包
{
for (int v=cost;v<=V;v++)
f[v]=min(f[v],f[v-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount,int f[]) //多重背包
{
if (cost*amount>=V)
{
CompletePack(cost,weight,f);
return ;
}
int k=;
while (k<amount)
{
ZeroOnePack(k*cost,k*weight,f);
amount=amount-k;
k*=;
}
ZeroOnePack(amount*cost,amount*weight,f);
}
void init(int M,int f[])
{
f[]=; //保证背包装满 具体原因参见背包九讲
for (int i=; i<=M; i++) //求最小价值要把初值赋值为正无穷(INT_MAX可能会导致整型溢出)
f[i]=INF;
}
int main()
{
while (cin>>N>>V)
{ int V2=V;
int max=;
for (int i=; i<=N; i++){
cin>>c[i];
if (c[i]>max) max=c[i];}
for (int i=; i<=N; i++)
cin>>a[i];
V=max*max+V2+; //要找钱,V要比T大很多才行
init(V,f1);
init(V,f2);
for (int i=;i<=N;i++)
MultiplePack(c[i],,a[i],f1);
for (int i=;i<=N;i++)
CompletePack(c[i],,f2);
int ans=INF;
for (int i=;i<=V-V2;i++)
if (f1[i+V2]!=INF&&f2[i]!=INF) ans=min(ans,f1[i+V2]+f2[i]);
if (ans!=INF) printf("%d\n",ans); //ans==INF表示数据没有变过,则表示无解
else printf("-1\n");
}
return ;
}
POJ3260——The Fewest Coins(多重背包+完全背包)的更多相关文章
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- POJ3260:The Fewest Coins(混合背包)
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- POJ3260 The Fewest Coins(混合背包)
支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...
- POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...
- poj3260 The Fewest Coins
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- The Fewest Coins POJ - 3260
The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...
- POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)
题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
随机推荐
- 编程之美 ---> 1.2中国象棋将帅问题
上图,将帅不能碰面,列出将帅不碰面的所有可能情况,要求:程序只能用一个只有8位的变量(#define这样的就不算了) 为了更加符合程序员的口味,给将帅位置编号如下: 0--1--2 | | ...
- wireshark总结
拖延了两个月的总结!下面的很大一部分来自其它博客. wireshark过滤器的区别 捕捉过滤器(CaptureFilters):用于决定将什么样的信息记录在捕捉结果中.需要在开始捕捉前设置.在Capt ...
- 【CLR VIA C#】读书笔记
工作几年了才看,记录下笔记备忘. 章节 笔记 1.CLR的执行模型 公共语言运行时(Common Language Runtime,CLR) 源代码-->编译器检查语法和分析源代码-->托 ...
- [Linux]学习笔记(3)-uname的用法
uname的用法如下: uname –a[--all]:输出全部信息 [root@linuxforlijiaman ~]# uname -a Linux linuxforlijiaman -.el6. ...
- php数组去重实例及分析
php数组去重实例及分析. 一维数组的重复项: 使用array_unique函数即可,使用实例 <?php $aa=array("apple","banan ...
- YII千万级PV架构经验分享--理论篇
hello,大家好,我是方少,现在想象一下这样一个情景,这是一个很惬意的季节,是一个可以随意乱穿的季节,两个人,一个穿羽绒服,一个穿热裤,小胡同里两人迎面走来,看到对方都哈哈大笑,前仰后合,笑完都甩一 ...
- .NET基础之迭代器
使用foreach循环是有IEnumerator接口来实现的,IEnumerator即实现了迭代器,在foreach中如何迭代一个集合arrayList呢? 调用arrayLis.GetEnumber ...
- 移植openssl
1.官网(ftp://ftp.openssl.org/source/old/0.9.x/)下载openssl-0.9.8k.tar.gz2.交叉编译openssl CC=arm-hisiv400-li ...
- Oracle中的CR块详解
1.概述 Cr块consistent read块也就是用来维护oracle的读一致性的数据块.当查询某些数据的时候,发现数据块的版本比我们要查询的新,例如session1执行了dml操作并没有提交,s ...
- wampserver安装后的基本配置
wampserver安装后的基本配置 1.WampServer的安装 下载好安装包后,你能在保存其文件夹中找到这样一个图标: 双击它,会弹出如下提示 提示信息:不要试图从Wamp5 1.x(x代表任意 ...