您的位置:首页 > 其它

The trouble of Xiaoqian

2015-08-17 15:30 246 查看
The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1472 Accepted Submission(s): 502

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.

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.

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

Sample Output

Case 1: 3

背包比较好的题,对于xiaoqian想要使经手的钱做少,所以它给店员的钱必定要T<=m<=20000,所以对于xiaoqian进行多重背包,计算付出m时,需要支付钱的数量最小值,对于店员,xiaoqian多给的钱需要找回,找回的钱的数量也要最少,店员的钱是没有限制的所以对店员进行完全背包

最后遍历找最小值

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;

const int INF = 0x3f3f3f3f;

const int MAX = 20001;

int dp[MAX];

int Dp[MAX];

int w[120];

int c[120];

int MIN;

int main()
{
int n,m;
int W=1;
while(scanf("%d %d",&n,&m)&&(n||m))
{
for(int i=0;i<n;i++)
{
scanf("%d",&w[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&c[i]);
}
memset(Dp,INF,sizeof(Dp));
memset(dp,INF,sizeof(dp));
Dp[0]=0;
dp[0]=0;
for(int i=0;i<n;i++)
{
int bite=1;
int num=c[i];
while(num)
{
num-=bite;
for(int j=MAX-1;j>=w[i]*bite;j--)
{
Dp[j]=min(Dp[j],Dp[j-w[i]*bite]+bite);
}
if(bite*2<num)
{
bite*=2;
}
else
{
bite=num;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=w[i];j<MAX;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+1);
}
}
MIN=INF;
for(int i=m;i<MAX;i++)
{
MIN=min(MIN,Dp[i]+dp[i-m]);
}
printf("Case %d: ",W++);
if(MIN==INF)
{
printf("-1\n");
}
else
{
printf("%d\n",MIN);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: