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. ...
随机推荐
- OCI下报出的数据库重账错
[2014-01-09 19:43:45.469220][22347888] Level 0 COCITOOL: Error - OCI_ERROE - errcode[1],errmsg[ORA-0 ...
- 教您如何使用MySQL group_concat函数
MySQL group_concat函数是典型的字符串连接函数,下面就为您介绍MySQL group_concat的语法,希望对您学习MySQL group_concat函数有所帮助. MySQL g ...
- HTML5 内联框架iFrame
由于现在frame和frameset很少使用,已经过时了,已经被div+CSS代替了,所以,这里只是举例说明一下,当下还在使用的内联框架iFrame 所谓的iFrame内联框架,我的理解就是在网页内部 ...
- laravel扩展Debugbar
github地址:https://github.com/barryvdh/laravel-debugbar
- Spark Streaming揭秘 Day19 架构设计和运行机制
Spark Streaming揭秘 Day19 架构设计和运行机制 今天主要讨论一些SparkStreaming设计的关键点,也算做个小结. DStream设计 首先我们可以进行一个简单的理解:DSt ...
- Linux学习系列之Linux入门(一)linux安装与入门
第一篇:安装并配置Linux开发环境 一.安装linux: 主要安装Linux的发行版,到目前为之,主要的发行版有: 比较常用的是Ubuntu.redhat和centOS,主要的安装方法详细: Ubu ...
- C# 将list<>泛型集合 转化为 DataTable
使用案例:将页面easy ui 中datagrid表格中的数据,存成json字符串, 通过ajax和ashx传入C#将string类型的json字符串解析成list<>泛型集合, 由于业务 ...
- SQL Server是如何让定时作业
如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理->SQL Server代理->作业来实现. 1.管理->SQL S ...
- Qt 内存管理机制(转)
许转载http://devbean.blog.51cto.com/448512/526734 强类型语言在创建对象时总会显式或隐式地包含对象的类型信息.也就是说,强类型语言在分配对象内存空间时,总 ...
- TOPAPI 消息通知机制
接收用户订阅消息 public class UserSubMain { public static void main(String[] args ) throws ApiException { St ...