您的位置:首页 > 其它

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: