The trouble of Xiaoqian
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1076 Accepted Submission(s): 355
Problem Description
In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.)
And now , Xiaoqian 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). Xiaoqian 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 .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
And now , Xiaoqian 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). Xiaoqian 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 .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
Input
There are several test cases in the 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
The end of the input is a double 0.
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
The end of the input is a double 0.
Output
Output one line for each test case like this ”Case X: Y” : X presents the Xth test case and Y presents the minimum number of coins . If it is impossible to pay and receive exact change, output -1.
Sample Input
3 70
5 25 50
5 2 1
0 0
5 25 50
5 2 1
0 0
Sample Output
Case 1: 3
多重背包.
代码:
#include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f;
struct node
{
int v,c;
};
node sta[];
int dp[];
int dp2[];
int main()
{
int n,t,i,j,maxc,cnt=; //开始cnt赋值在while里面,娘希匹,错了10+
while(scanf("%d%d",&n,&t),n+t)
{
maxc=-inf; for(i=;i<n;i++)
{
scanf("%d",&sta[i].v);
if(maxc<sta[i].v) maxc=sta[i].v;
}
for(i=;i<n;i++)
scanf("%d",&sta[i].c);
maxc+=t;
for(i=;i<=maxc+;i++)
dp[i]=inf;
dp[]=;
for(i=;i<n;i++)
{
if(sta[i].v*sta[i].c>=t) /*完全背包*/
{
for(j=sta[i].v ; j<=maxc ;j++)
{
if(dp[j]>dp[j-sta[i].v]+)
dp[j]=dp[j-sta[i].v]+;
}
}
else
{
int k=;
while(sta[i].c>k)
{
for( j=maxc ; j>=sta[i].v*k ; j-- )
{
if(dp[j]>dp[j-sta[i].v*k]+k)
dp[j]=dp[j-sta[i].v*k]+k;
}
sta[i].c-=k;
k<<=;
}
for( j=maxc; j>=sta[i].c*sta[i].v ; j-- )
{
if(dp[j]>dp[j-sta[i].v*sta[i].c]+sta[i].c)
dp[j]=dp[j-sta[i].v*sta[i].c]+sta[i].c;
}
} }
for(i=;i<=maxc+;i++)
dp2[i]=inf;
dp2[]=;
for(i=;i<n;i++)
{
for(j=sta[i].v ;j<=maxc;j++)
{
if(dp2[j]>dp2[j-sta[i].v]+)
dp2[j]=dp2[j-sta[i].v]+ ;
}
}
int ans=inf;
for(i=t;i<=maxc ;i++)
{
if(ans>dp[i]+dp2[i-t]) ans=dp[i]+dp2[i-t];
}
if(ans==inf) printf("Case %d: -1\n",cnt++);
else
printf("Case %d: %d\n",cnt++,ans); }
return ;
}
第二种...
#include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f;
struct node
{
int v,c;
};
node sta[];
int dp[];
int dp2[];
int main()
{
int n,t,i,j,maxc,cnt=;
while(scanf("%d%d",&n,&t),n+t)
{
maxc=-inf;
for(i=;i<n;i++)
{
scanf("%d",&sta[i].v);
if(maxc<sta[i].v) maxc=sta[i].v;
}
for(i=;i<n;i++)
scanf("%d",&sta[i].c);
maxc+=t;
memset(dp,-,sizeof(dp[])*(maxc+));
dp[]=;
for(i=;i<n;i++)
{
if(sta[i].v*sta[i].c>=t) /*完全背包*/
{
for(j=sta[i].v ; j<=maxc ;j++)
{
if(dp[j-sta[i].v]!=-&&(dp[j]==-||dp[j]>dp[j-sta[i].v]+))
dp[j]=dp[j-sta[i].v]+;
}
}
else
{
int k=;
while(sta[i].c>k)
{
for( j=maxc ; j>=sta[i].v*k ; j-- )
{
if(dp[j-sta[i].v*k]!=-&&(dp[j]==-||dp[j]>dp[j-sta[i].v*k]+k))
dp[j]=dp[j-sta[i].v*k]+k;
}
sta[i].c-=k;
k<<=;
}
for( j=maxc; j>=sta[i].c*sta[i].v ; j-- )
{
if(dp[j-sta[i].v*sta[i].c]!=-&&(dp[j]==-||dp[j]>dp[j-sta[i].v*sta[i].c]+sta[i].c))
dp[j]=dp[j-sta[i].v*sta[i].c]+sta[i].c;
}
} }
memset(dp2,-,sizeof(dp2[])*(maxc+));
dp2[]=;
for(i=;i<n;i++)
{
for(j=sta[i].v ;j<=maxc;j++)
{
if(dp2[j-sta[i].v]!=-&&(dp2[j]==-||dp2[j]<dp2[j-sta[i].v]+))
dp2[j]=dp2[j-sta[i].v]+ ;
}
}
int ans=inf;
for(i=t;i<=maxc ;i++)
{
if(dp2[i-t]!=-&&dp[i]!=-&&ans>dp[i]+dp2[i-t])
ans=dp[i]+dp2[i-t];
}
if(ans==inf) printf("Case %d: -1\n",cnt++);
else
{
printf("Case %d: %d\n",cnt++,ans);
} }
return ;
}