hdu 3591 The trouble of Xiaoqian 多重背包(二进制)+完全背包!

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3591


The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1476    Accepted Submission(s): 504


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的东西, 商家和她有价值为V1,V2....VN的硬币, 其中商家每种价值的

硬币有无限多个, 而xiaoqian每种相应有C1,C2...CN个。  问 xiaoqian买价值为T的东西, 所需要的最少的硬币数, 包括商家找回的个数。



思路:只用先对xiaoqian进行分组背包(注意这里需要用到二进制的思维),  再对商家进行完全背包即可。



#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <functional>
#include <cstring>

#define UFOR(i, a, b) for(int i = a; i <= b; i++)
#define DFOR(i, a, b) for(int i = a; i >= b; i--)
#define MEM0(a) memset(a, 0, sizeof(a))
#define MEM1(a) memset(a, -1, sizeof(a))
#define MEMINF(a) memset(a, 0x3f3f3f3f, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)

using namespace std;

//typedef __int64 LL;
typedef long long LL;
const int MXN = 2e3 + 10;
const int MXM = 2e4 + 130;
const int HS = 1000007;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;


int dp1[MXM], dp2[MXM], a[130], v[MXN], num[MXN];


int main ()
{
     int n, T, kase = 1;
     while (~scanf("%d %d", &n, &T), n + T) {
          MEMINF(dp1);
          MEMINF(dp2);
          UFOR (i, 1, n) scanf("%d", &a[i]);

          int tot = 1;
          UFOR (i, 1, n) {
               int k;
               scanf("%d", &k);

               int cnt = 0;
               while (k - (1 << cnt) >= 0) {
                    num[tot] = (1 << cnt);
                    v[tot++] = a[i];
                    k -= (1 << cnt);
                    cnt++;
               }

               if (k) {
                    num[tot] = k;
                    v[tot++] = a[i];
               }
          }


          dp1[0] = 0;
          UFOR (i, 1, tot - 1) {
               DFOR (j, 20000, v[i] * num[i]) {
                    dp1[j] = min(dp1[j], dp1[j - num[i] * v[i]] + num[i]);
               }
          }

       //   printf("%d\n", dp1[T]);                                   //debug

          dp2[0] = 0;
          UFOR (i, 1, n) {
               UFOR (j, a[i], 20000) {             //注意这里也要跑到20000..
                    dp2[j] = min(dp2[j], dp2[j - a[i]] + 1);
               }
          }


          int ans = INF;
          UFOR (i, T, 20000) {
               if (dp1[i] < INF) ans = min(ans, dp1[i] + (i > T ? dp2[i - T] : 0));
          }

          printf("Case %d: %d\n", kase++, ans < INF ? ans : -1);
     }
     return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值