The trouble of Xiaoqian
2013-08-04 18:10
211 查看
O-ThetroubleofXiaoqian
TimeLimit:1000MSMemoryLimit:32768KB64bitIOFormat:%I64d&%I64u
Submit
Description
InthecountryofALPC,Xiaoqianisaveryfamousmathematician.Sheisimmersedincalculate,andshewanttousetheminimumnumberofcoinsineveryshopping.(Thenumbersoftheshoppingincludethecoinsshegavethestoreandthestorebackedtoher.)
Andnow,XiaoqianwantstobuyT(1≤T≤10,000)centsofsupplies.ThecurrencysystemhasN(1≤N≤100)differentcoins,withvaluesV1,V2,...,VN(1≤Vi≤120).XiaoqianiscarryingC1coinsofvalueV1,C2coinsofvalueV2,....,andCNcoinsofvalueVN(0≤Ci≤10,000).Theshopkeeperhasanunlimitedsupplyofallthecoins,andalwaysmakeschangeinthemostefficientmanner.ButXiaoqianisalow-pitchedgirl,shewouldn’tlikegivingoutmorethan20000once.
Input
Thereareseveraltestcasesintheinput.
Line1:Twospace-separatedintegers:NandT.
Line2:Nspace-separatedintegers,respectivelyV1,V2,...,VNcoins(V1,...VN)
Line3:Nspace-separatedintegers,respectivelyC1,C2,...,CN
Theendoftheinputisadouble0.
Output
Outputonelineforeachtestcaselikethis”CaseX:Y”:XpresentstheXthtestcaseandYpresentstheminimumnumberofcoins.Ifitisimpossibletopayandreceiveexactchange,output-1.
SampleInput
370
52550
521
00
SampleOutput
Case1:3
(多重背包+分组背包)
#include<iostream>
#include<cstdio>
#include<cstring>
#defineMaxn999999
usingnamespacestd;
intw[105],num[105],sum;
intdp1[22000];
intdp2[22000];
intmain()
{
inti,k,j;
intn,T,cas=0;
while(scanf("%d%d",&n,&T)!=EOF&&(n||T))
{
for(i=0;i<n;i++)
scanf("%d",&w[i]);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
memset(dp2,63,sizeof(dp2));
dp2[0]=0;
for(i=0;i<n;i++)
for(j=w[i];j<=20000;j++)
dp2[j]=min(dp2[j],dp2[j-w[i]]+1);
memset(dp1,63,sizeof(dp1));
dp1[0]=0;
for(i=0;i<n;i++)
{
inttmp=num[i];
for(k=1;k<=tmp;k*=2)
{
if(num[i]<k)k=num[i];
ints=k*w[i];
for(j=20000;j>=s;j--)
dp1[j]=min(dp1[j],dp1[j-s]+k);
num[i]-=k;
if(num[i]==0)
break;
}
}
cout<<"Case"<<++cas<<":";
intans=Maxn;
for(i=T;i<=20000;i++)
ans=min(ans,dp1[i]+dp2[i-T]);
if(ans==Maxn)
printf("-1\n");
else
printf("%d\n",ans);
}
return0;
}
相关文章推荐
- HDU3591 - The trouble of Xiaoqian(多重背包+完全背包)
- HDU3591:The trouble of Xiaoqian(多重背包+完全背包)
- hdu3591 The trouble of Xiaoqian(多重背包 + 完全背包)
- hdu 3591 The trouble of Xiaoqian(多重背包)
- The trouble of Xiaoqian
- hdu 3591 The trouble of Xiaoqian 多重背包+完全背包
- The trouble of Xiaoqian
- hdu 3591 The trouble of Xiaoqian 多重背包(二进制)+完全背包!
- HDU 3591 The trouble of Xiaoqian (多重背包+完全背包)
- 【HDU 3591】The trouble of Xiaoqian
- HDU 3594 The trouble of Xiaoqian 混合背包问题
- 【HDU 3591】The trouble of Xiaoqian
- HDU 3591 The trouble of Xiaoqian(多重背包+完全背包)
- 【HDU 3591】The trouble of Xiaoqian
- hdu 3591 The trouble of Xiaoqian(多重背包+完全背包)
- hdu 3591 The trouble of Xiaoqian( 多重背包+完全背包)
- HDU 3591 The trouble of Xiaoqian 混合背包(完全背包和多重背包混合)
- hdu 3591 The trouble of Xiaoqian(多重背包)
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
- HDU 3591 The trouble of Xiaoqian(多重背包+完全背包)