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(多重背包+完全背包)的更多相关文章

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

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

  2. POJ3260:The Fewest Coins(混合背包)

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  3. POJ3260 The Fewest Coins(混合背包)

    支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...

  4. POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)

    Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...

  5. poj3260 The Fewest Coins

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  6. The Fewest Coins POJ - 3260

    The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...

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

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

  8. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  9. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

随机推荐

  1. Poj 2109 / OpenJudge 2109 Power of Cryptography

    1.Link: http://poj.org/problem?id=2109 http://bailian.openjudge.cn/practice/2109/ 2.Content: Power o ...

  2. sed- 文本流编辑器

    sed                    [选项]                                             [参数] -n   被操作行打印输出           ...

  3. OpenCV3读取、写入和保存图像

    需要说明的是在OpenCV3中已经将imread()和imwrite()函数转移到imgcodecs模块中,因此读写图像时,需要包含imgcodecs.hpp头文件,但是highgui.hpp头文件中 ...

  4. 开发问题记录——ArcEngine问题记录

    ArcEngine 使用Winform进行坐标投影变换,用到AE空间,出现如下错误:   “ESRI.ArcGIS.esriSystem.IXMLSerialize”在未被引用的程序集中定义.必须添加 ...

  5. PL/SQL Developer编码格式设置及中文乱码解决方案

    1.PL/SQL Developer中文字段显示乱码 原因:因为数据库的编号格式和pl /sql developer的编码格式不统一造成的. 2.PL/SQL Developer编码格式设置详细的解决 ...

  6. Centos7搭建集中式日志系统

    在CentOS7中,Rsyslong是一个集中式的日志收集系统,可以运行在TCP或者UDP的514端口上.   目录 开始之前 配置接收日志的主机 配置发送日志的主机 日志回滚 附件:创建日志接收模板 ...

  7. IEEE 754 浮点数的四种舍入方式

    四种舍入方向: 向最接近的可表示的值:当有两个最接近的可表示的值时首选"偶数"值:向负无穷大(向下):向正无穷大(向上)以及向0(截断). 说明:默认模式是最近舍入(Round t ...

  8. Mac 启用http-dav功能(WebDAV服务器)

    启用Mac的WebDAV可以实现文件的上传以及Windows.Linux和Mac之间的数据互传. 客户端使用:windows下使用网上邻居 --> 添加一个网上邻居  --> 输入uplo ...

  9. Linux操作系统

    Linux操作系统 linux源码分析(三)-start_kernel 2016-10-26 11:01 by 轩脉刃, 146 阅读, 收藏, 编辑 前置:这里使用的linux版本是4.8,x86体 ...

  10. sql之表的表达式

    1.派生表 实质:就是特殊的子查询(将查询结果放在from后面) 含有一张Order表: 看下面的sql语句: select orderid,orderdate,custid from ( selec ...