题目代号:POJ 3260

题目链接:http://poj.org/problem?id=3260

The Fewest Coins

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6715 Accepted: 2072

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

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.

题目大意:给你N种面值的钱以及买家所拥有的数量(注意:卖家的没种面值的钱是无限的),要买T价值的东西,问:要使给出的钱和收到的钱数量最小,然后求总和就是答案。

题目分析:对于买家来说,这是一个多重背包问题,对于卖家来说,这是一个完全背包问题,所以这个题是多重背包和完全背包组合而成的混合背包问题,由于背包问题都是由01背包衍生和优化出来的,所以需要对于01背包的思想完全掌握和熟练运用。然后另一个问题就是这个背包容量的上限问题,在我们生活之中如果需要买1269元的东西那么我们需要给出12张100元面值的,1张50元面值的,1张10元面值的,1张5元面值的,4张1元面值的,合计19张,那么如果按照题目中的要求我可以付款13~18张的100面值的钱,18显然比19要少,但是给了超过1300元的面值的100元都会被卖家原样找回,这很显然是没有必要的,因为在最优的答案之中找的钱不会超过100元,这个题目中也是这样,然后根据抽屉原理,多重背包容量的上限为T+max*max(max即为最大面值的钱),完全背包的上限为T+max。(别问我,我也不知道怎么来的,记住就好了,实在不会那就数组开大点。。。)

AC代码:

# include <bits/stdc++.h>
using namespace std;
# define mem(a,b) memset(a,b,sizeof(a))
# define IOS ios::sync_with_stdio(false);
# define FO(i,n,a) for(int i=n; i>=a; --i)
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define MAX 0x7fffffffffffff
# define INF 0x3f3f3f3f
# define MOD 1000000007
/// 123456789
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef unsigned long long ULL;
typedef long long LL;
///coding...................................
const int MAXM=30005;
int n,m,c[105],v[105],sum;
int b[MAXM],a[MAXM]; void complete_bag(int *dp,int vis) {
FOR(i,vis,sum)
dp[i]=min(dp[i],dp[i-vis]+1);
} void zero_one_bag(int *dp,int vis,int cost) {
FO(i,sum,vis)
dp[i]=min(dp[i],dp[i-vis]+cost);
} void mult_bag(int *dp,int vis,int cost) {
if(vis*cost>=sum)complete_bag(dp,vis);
else{
int cnt=1;
while(cnt<=cost){
zero_one_bag(dp,vis*cnt,cnt);
cost-=cnt;
cnt<<=1;
}
if(cost)zero_one_bag(dp,vis*cost,cost);
}
} int main()
{
IOS
#ifdef FLAG
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif /// FLAG
while(cin>>n>>m) {
sum=0;
for(int i=0;i<n;i++)
cin>>v[i],sum=max(sum,v[i]);
for(int i=0;i<n;i++)cin>>c[i];
sum=m+sum*sum;
for(int i=1;i<=sum;i++)a[i]=b[i]=INF;
b[0]=a[0]=0;
for(int i=0;i<n;i++)complete_bag(a,v[i]);
for(int i=0;i<n;i++)mult_bag(b,v[i],c[i]);
int ans=INF;
for(int i=m;i<=sum;i++)
if(b[i]+a[i-m]<ans)
ans=b[i]+a[i-m];
if(ans==INF)puts("-1");
else cout<<ans<<endl;
}
return 0;
}

POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)的更多相关文章

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

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

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

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

  3. poj 3260 The Fewest Coins

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

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

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

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

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

  6. codevs 3269 混合背包(复习混合背包)

    传送门 [题目大意]给出物品的数量.-1为无限个. [思路]混合背包.... [code] #include<iostream> #include<cstdio> #inclu ...

  7. The Fewest Coins POJ - 3260

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

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

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

  9. POJ 3260 多重背包+完全背包

    前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...

随机推荐

  1. HDU 1114 Piggy-Bank(动态规划、完全背包)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. Python流程控制与while 循环(day01)

    一:流程控制 假如把写程序比做走路,那我们到现在为止,一直走的都是直路,还没遇到过分叉口,想象现实中,你遇到了分叉口,然后你决定往哪拐必然是有所动机的.你要判断哪条叉路是你真正要走的路,如果我们想让 ...

  3. 洛谷 P1972 HH的项链 题解

    题面 本题其实主要就这几点: 1.离线,以右端点排序(从小到大); 2.建立树状数组c[],c[i]表示从1~i中有多少种不同的数字: 3.对于每次查询的答案就是sum(r)-sum(l-1); 4. ...

  4. [UER #1] DZY Loves Graph

    题目描述 开始有 \(n\) 个点,现在对这 \(n\) 个点进行了 \(m\) 次操作,对于第 \(i\) 个操作(从 \(1\) 开始编号)有可能的三种情况: \(Add\) a b: 表示在 \ ...

  5. Head First PHP&MySQl第三章代码

    addemail.html <!DOCTYPE html> <html lang="cn"> <head> <meta charset=& ...

  6. SIGCHLD和wait/waipid函数的关系

    SIGCHILD只是在子进程退出的时候发送给父进程的一个信号值,这是一种异步通知父进程的方式.父进程可以捕获,忽略这个信号,默认动作是忽略此信号. 常用的使用方式是,当SIGCHILD信号发生时候,主 ...

  7. day16 常用模块 sys os json pickle

          知识点 os:和操作系统相关sys:和解释器相关 json:和操作JSON(一种数据交换格式)相关pickle:序列化 hashlib:加密算法Collections:集合类型       ...

  8. 【golang】浅析rune数据类型

    golang中string底层是通过byte数组实现的.中文字符在unicode下占2个字节,在utf-8编码下占3个字节,而golang默认编码正好是utf-8. golang中还有一个byte数据 ...

  9. 2.学习Application

    2学习Application Application对象事件 名称 说明 Activated 当应用程序成为前台应用程序时触发 Deactivated 当应用程序不再是前台应用程序时触发 Dispat ...

  10. vue在组件中使用v-model

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...