poj 3260 The Fewest Coins(多重背包+完全背包)
2011-10-06 14:10
288 查看
The Fewest Coins
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 |
分析:这题很像之前做过的一题,不过这题变成了完全背包,这题的关键还是那个付钱范围的估计,网上有人说是maxV*maxV+T,不过我不会估计= =,反正就是这样过的。。。不过数据还想最大到T左右就能过。。。
代码:
#include<cstdio> #include<iostream> using namespace std; const int mm=24222; const int mn=111; int v[mn],c[mn]; int f[mm],g[mm]; int n,m,mv,mx; void CompletePack(int v) { for(int i=v;i<=mv;++i)f[i]=min(f[i],f[i-v]+1); } void ZeroOnePack(int v,int d) { for(int i=mv;i>=v;--i)f[i]=min(f[i],f[i-v]+d); } int main() { int i,j; while(scanf("%d%d",&n,&m)!=-1) { for(mx=i=0;i<n;++i)scanf("%d",&v[i]),mx=max(mx,v[i]); for(i=0;i<n;++i)scanf("%d",&c[i]); mx=mx*mx; mv=m+mx; for(i=0;i<=mv;++i)f[i]=g[i]=mm; f[0]=g[0]=0; for(i=0;i<n;++i) if(c[i]) { if(c[i]*v[i]>=mv)CompletePack(v[i]); else { j=1; while(j<c[i]) { ZeroOnePack(v[i]*j,j); c[i]-=j; j<<=1; } ZeroOnePack(v[i]*c[i],c[i]); } } for(i=0;i<n;++i) for(j=v[i];j<=mx;++j) g[j]=min(g[j],g[j-v[i]]+1); for(i=m;i<=mv;++i)f[m]=min(f[m],f[i]+g[i-m]); printf("%d\n",f[m]<mm?f[m]:-1); } return 0; }
相关文章推荐
- POJ 3260 - The Fewest Coins(完全背包+多重背包)
- POJ - 3260 The Fewest Coins (完全背包)
- poj 3260 The Fewest Coins 多重背包+完全背包
- POJ3260 The Fewest Coins ——完全背包+多重背包——Pku3260
- POJ 3260 The Fewest Coins (混合背包--多重背包+完全背包)
- 背包问题--POJ 3260 The Fewest Coins【完全背包+多重背包】
- POJ 3260 The Fewest Coins(多重背包+完全背包—>交易数量最小)@
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins(完全背包 + 多重背包 + 单调队列优化)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins(完全背包+多重背包(转化为01背包))
- POJ3260——The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)
- poj 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ3260_The Fewest Coins_deque优化dp解多重背包和完全背包
- POJ 3260 The Fewest Coins 多重背包+完全背包
- poj 3260 The Fewest Coins (多重背包 + 完全背包)
- POJ 3260 The Fewest Coins (多重背包 + 完全背包)
- POJ 3260 The Fewest Coins / 混合背包