poj 3260(多重+完全背包)
2016-04-10 16:27
405 查看
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
Sample Output
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.
Source
USACO 2006 December Gold
多重背包求解得到数T(此T非题目给的T因为可以找零所以可以大于它)的最小代价 完全背包求解找零后的最小代价 前面T开小了直接wa 一怒之下直接标到200000
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6030 | Accepted: 1807 |
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.
Source
USACO 2006 December Gold
多重背包求解得到数T(此T非题目给的T因为可以找零所以可以大于它)的最小代价 完全背包求解找零后的最小代价 前面T开小了直接wa 一怒之下直接标到200000
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; const int inf=(1<<30); int a[200]; int num[200]; int dp[210015]; int n,t; void ZeroOnePack(int w,int v) { for(int i=200000;i>=w;i--) if(dp[i-w]+v<dp[i]) dp[i]=dp[i-w]+v; } void CompletePack(int w,int v) { for(int i=w;i<=200000;i++) if(dp[i-w]+v<dp[i]) dp[i]=dp[i-w]+v; } void MultiplePack() { for(int i=1;i<=n;i++) { if(a[i]*num[i]>200000) CompletePack(a[i],1); else { int k=1; while(k<num[i]) { ZeroOnePack(a[i]*k,k); num[i]-=k; k*=2; } ZeroOnePack(a[i]*num[i],num[i]); } } } int main() { while(~scanf("%d%d",&n,&t)) { for(int i=1;i<=210005;i++) dp[i]=inf; dp[0]=0; for(int i=1;i<=n;i++) scanf("%d",a+i); for(int i=1;i<=n;i++) scanf("%d",num+i); MultiplePack(); //可以凑到的数 for(int i=1;i<=n;i++) //完全背包找零 for(int k=200000;k>=1;k--) if(dp[k+a[i]]+1<dp[k]) dp[k]=dp[k+a[i]]+1; if(dp[t]==inf) puts("-1"); else printf("%d\n",dp[t]); } return 0; }
相关文章推荐
- Python学习笔记第一天
- VB.net机房收费系统小问题之变量赋值前被使用
- 随机取10个数第九种
- (python解析js)scrapy结合ghost抓取js生成的页面,以及js变量的解析
- MongoDB 决策分析
- foreach和for循环的区别
- 【01背包】HDU5656CA Loves GCD
- 关于% 探究
- 外部中断
- 转载:JAVA的StringBuffer类 详解
- hdu5663Hillan and the girl
- 2章 Java基础语法
- foreach和for循环的区别
- hdu-2586 How far away ?(lca+bfs+dfs+线段树)
- 从零开始学_JavaScript_系列(七)——jquery(复选框及互斥、div块、修改css、标签数组、ajax连续加载)
- uva679
- 祖国万岁——2
- Linux C语言 UDP
- UVa 10129 Play on Words 欧拉回路 解题报告
- 祖国万岁——1