POJ1742:Coins(多重背包)
2013-07-27 10:42
363 查看
Description
PeopleinSilverlandusecoins.TheyhavecoinsofvalueA1,A2,A3...AnSilverlanddollar.OnedayTonyopenedhismoney-boxandfoundthereweresomecoins.Hedecidedtobuyaverynicewatchinanearbyshop.Hewantedtopaythe
exactprice(withoutchange)andheknownthepricewouldnotmorethanm.Buthedidn'tknowtheexactpriceofthewatch.
Youaretowriteaprogramwhichreadsn,m,A1,A2,A3...AnandC1,C2,C3...CncorrespondingtothenumberofTony'scoinsofvalueA1,A2,A3...Anthencalculatehowmanyprices(form1tom)Tonycanpayusethesecoins.
Input
Theinputcontainsseveraltestcases.Thefirstlineofeachtestcasecontainstwointegersn(1<=n<=100),m(m<=100000).Thesecondlinecontains2nintegers,denotingA1,A2,A3...An,C1,C2,C3...Cn(1<=Ai<=100000,1<=Ci<=1000).The
lasttestcaseisfollowedbytwozeros.
Output
Foreachtestcaseoutputtheansweronasingleline.
SampleInput
SampleOutput
PeopleinSilverlandusecoins.TheyhavecoinsofvalueA1,A2,A3...AnSilverlanddollar.OnedayTonyopenedhismoney-boxandfoundthereweresomecoins.Hedecidedtobuyaverynicewatchinanearbyshop.Hewantedtopaythe
exactprice(withoutchange)andheknownthepricewouldnotmorethanm.Buthedidn'tknowtheexactpriceofthewatch.
Youaretowriteaprogramwhichreadsn,m,A1,A2,A3...AnandC1,C2,C3...CncorrespondingtothenumberofTony'scoinsofvalueA1,A2,A3...Anthencalculatehowmanyprices(form1tom)Tonycanpayusethesecoins.
Input
Theinputcontainsseveraltestcases.Thefirstlineofeachtestcasecontainstwointegersn(1<=n<=100),m(m<=100000).Thesecondlinecontains2nintegers,denotingA1,A2,A3...An,C1,C2,C3...Cn(1<=Ai<=100000,1<=Ci<=1000).The
lasttestcaseisfollowedbytwozeros.
Output
Foreachtestcaseoutputtheansweronasingleline.
SampleInput
310 124211 25 1421 00
SampleOutput
8 4
这道题与杭电的一道题的题意是相同的,之所以拿出来是因为杭电那道题的代码在这里过不了,所以贴一个新的代码
#include<stdio.h>
#include<algorithm>
#include<string.h>
intdp[100005];
intsum[100005];
intv[105],c[105];
intmain()
{
inti,j,k,n,m;
while(~scanf("%d%d",&n,&m),n+m)
{
for(i=1;i<=n;i++)
scanf("%d",&v[i]);
for(i=1;i<=n;i++)
scanf("%d",&c[i]);
memset(dp,0,sizeof(dp));
dp[0]=1;
intans=0;
for(i=1;i<=n;i++)
{
memset(sum,0,sizeof(sum));
for(j=v[i];j<=m;j++)
{
if(!dp[j]&&dp[j-v[i]]&&sum[j-v[i]]<c[i])
{
dp[j]=1;
sum[j]=sum[j-v[i]]+1;
ans++;
}
}
}
printf("%d\n",ans);
}
return0;
}
相关文章推荐
- POJ 1742 - Coins (dp 多重背包)
- POJ 1742 Coins( 单调队列优化多重背包)
- poj 1742 Coins(多重背包)
- poj 1742 Coins(多重背包)
- (优化的有限背包) poj 1742 Coins
- POJ 1742 Coins (背包)
- POJ 1742 Coins (多重背包的可行解 o(nm))
- Poj 1742 Coins(多重背包)
- 【POJ】1742 coins 【背包问题】
- POJ 1742 && HDU 2844 Coins(多重背包问题)
- POJ 1742 Coins(多重背包可行性问题)
- POJ 1742 Coins (多重背包)
- POJ1742 Coins(背包)
- poj1742 Coins 动态规划 多重背包 待补完
- poj 1742 coins_多重背包
- POJ1742 Coins(多重背包可行性)
- POJ 1742 Coins 多重背包(单调队列优化)
- POJ 1742 Coins 单调队列多重背包
- (优化的有限背包) poj 1742 Coins(转载)
- Poj 1742 Coins(多重背包)