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;
}

The trouble of Xiaoqian的更多相关文章

  1. hdu 3591 The trouble of Xiaoqian

    hdu 3591  The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西 ...

  2. HDUOJ-----3591The trouble of Xiaoqian

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  3. HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

    HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...

  4. HDU 3594 The trouble of Xiaoqian 混合背包问题

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  5. hdu3591The trouble of Xiaoqian 多重背包+全然背包

    //给出Xiaoqian的钱币的价值和其身上有的每种钱的个数 //商家的每种钱的个数是无穷,xiaoqian一次最多付20000 //问如何付钱交易中钱币的个数最少 //Xiaoqian是多重背包 / ...

  6. HDU - 3591 The trouble of Xiaoqian 题解

    题目大意 有 \(N\) 种不同面值的硬币,分别给出每种硬币的面值 \(v_i\) 和数量 \(c_i\).同时,售货员每种硬币数量都是无限的,用来找零. 要买价格为 \(T\) 的商品,求在交易中最 ...

  7. hdu 3591 多重加完全DP

    题目: The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  8. HDU 3591 (完全背包+二进制优化的多重背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...

  9. HDU_3591_(多重背包+完全背包)

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

随机推荐

  1. 【转】线程、Thread类和线程终止

    一.线程Thread启动 0. Thread类实现了java.lang.Runnable接口,即实现了run方法.虽然在Sun JDK中,start()调用了start0()方法,start0()方法 ...

  2. css less

    LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS的赋予了动态语言的特性,如变量.继承.运算.函数等,更方便CSS的编写和维护. LESSCSS可以在多种语 ...

  3. codeforces343A A. Rational Resistance

    http://http://codeforces.com/problemset/problem/343/A A. Rational Resistance time limit per test 1 s ...

  4. 【Origin】答友朋关切书

    发烧感冒脑袋疼, 剃了短毛不威风: 莫再问我有何事, 躺下一觉到天明. --作于二零一五年七月二十七日

  5. 当android studio一直显示gradle compile dependency

    出现这种情况,是被墙的问题,我的解决办法是这样的: 打开file---->setting,然后搜索gradle,把offline勾上,然后点击apply以及ok,就可以了. 有时候它会关闭,只需 ...

  6. 创建一个web Test Plan

    1.添加ThreadGroup (1).线程组界面解析: 线程数:虚拟用户的个数 Ramp-up Period:开启每个用户的延迟时间,如果有5个虚拟用户,Ramp-up Period值是5,Jmet ...

  7. java中时间的比较

    进入要比较从库中取出的时间,期初使用比较date.getTime()的值,但是当时间的年月日都相同时,时分秒较早的getTime()值比时分秒较晚的getTime()的值要大,至今笔者还不知这是为什么 ...

  8. explode and implode

    [PHP源码阅读]explode和implode函数   explode和implode函数主要用作字符串和数组间转换的操作,比如获取一段参数后根据某个字符分割字符串,或者将一个数组的结果使用一个字符 ...

  9. 05---Net基础加强

    接口 public class Program { static void Main(string[] args) { IFlyable sp = new SupperMan(); sp.Fly(); ...

  10. fnd_profile.value('AFLOG_ENABLED')的取值 和配置文件相关SQL

    SELECT * FROM FND_PROFILE_OPTIONS_VL TT WHERE TT.PROFILE_OPTION_NAME LIKE '%AFLOG%' FND:启用调试日志 详细的参考 ...