Relocation
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2631   Accepted: 1075

Description

Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything a bit. Since the furniture does not fit into the cars, Eric wants to put them on top of the cars. However, both cars only support a certain weight on their roof, so they will have to do several trips to transport everything. The schedule for the move is planed like this:

  1. At their old place, they will put furniture on both cars.
  2. Then, they will drive to their new place with the two cars and carry the furniture upstairs.
  3. Finally, everybody will return to their old place and the process continues until everything is moved to the new place.

Note, that the group is always staying together so that they can have more fun and nobody feels lonely. Since the distance between the houses is quite large, Eric wants to make as few trips as possible.

Given the weights wi of each individual piece of furniture and the capacities C1 and C2 of the two cars, how many trips to the new house does the party have to make to move all the furniture? If a car has capacity C, the sum of the weights of all the furniture it loads for one trip can be at most C.

Input

The first line contains the number of scenarios. Each scenario consists of one line containing three numbers nC1 and C2C1 and C2 are the capacities of the cars (1 ≤ Ci ≤ 100) and n is the number of pieces of furniture (1 ≤ n ≤ 10). The following line will contain n integers w1, …, wn, the weights of the furniture (1 ≤ wi ≤ 100). It is guaranteed that each piece of furniture can be loaded by at least one of the two cars.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line with the number of trips to the new house they have to make to move all the furniture. Terminate each scenario with a blank line.

Sample Input

  1. 2
  2. 6 12 13
  3. 3 9 13 3 10 11
  4. 7 1 100
  5. 1 2 33 50 50 67 98

Sample Output

  1. Scenario #1:
  2. 2
  3.  
  4. Scenario #2:
  5. 3

Source

TUD Programming Contest 2006, Darmstadt, Germany
 
题目意思:
有两辆容量分别为c1和c2的车,有n个体积为v[i]的物品,现用车拉物品,最少用多少次把所有物品拉完。
 
思路:
状压物品,记录所有可以一次性拉的物品组合二进制状态,那么答案肯定是由这些记录过的状态组合而成的,dp即可。
 
代码:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. #include <queue>
  7. #include <cmath>
  8. #include <set>
  9. using namespace std;
  10.  
  11. #define N 100005
  12. #define inf 999999999
  13.  
  14. int n, c1, c2;
  15. int w[];
  16. int num[];
  17.  
  18. bool solve(int nn){
  19. int i, j, k;
  20. int sum=;
  21. bool visited[];
  22. memset(visited,false,sizeof(visited));
  23. visited[]=true;
  24. for(i=;i<n;i++){
  25. if((<<i)&nn){
  26. sum+=w[i];
  27. for(j=c1-w[i];j>=;j--){//DP
  28. if(visited[j])
  29. visited[j+w[i]]=true;
  30. }
  31. }
  32. }
  33. for(i=;i<=c1;i++){
  34. if(visited[i]&&sum-i<=c2)
  35. return true;
  36. }
  37. return false;
  38. }
  39.  
  40. main()
  41. {
  42. int t, i, j, k;
  43. int kase=;
  44. cin>>t;
  45. while(t--){
  46. scanf("%d %d %d",&n,&c1,&c2);
  47. for(i=;i<n;i++) scanf("%d",&w[i]);
  48. int len=;
  49. int cnt=(<<n)-;
  50. for(i=;i<=cnt;i++){
  51. if(solve(i))
  52. num[len++]=i;
  53. }
  54. int dp[];
  55. for(i=;i<=cnt;i++) dp[i]=inf;
  56. dp[]=;
  57. for(i=;i<len;i++){//DP
  58. for(j=cnt;j>=;j--){
  59. if(!(j&num[i])){
  60. dp[j|num[i]]=min(dp[j|num[i]],dp[j]+);
  61. }
  62. }
  63. }
  64. printf("Scenario #%d:\n%d\n\n",kase++,dp[cnt]);
  65. }
  66. }

POJ 2923 状压好题的更多相关文章

  1. poj 2923 状压dp+01背包

    好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...

  2. POJ 3254 (状压DP) Corn Fields

    基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...

  3. POJ 3254 状压DP

    题目大意: 一个农民有一片n行m列 的农场   n和m 范围[1,12]  对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...

  4. poj 1170状压dp

    题目链接:https://vjudge.net/problem/POJ-1170 题意:输入n,表示有那种物品,接下来n行,每行a,b,c三个变量,a表示物品种类,b是物品数量,c代表物品的单价.接下 ...

  5. POJ 2411 状压DP经典

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16771   Accepted: 968 ...

  6. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  7. POJ 3254 状压DP(基础题)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17749   Accepted: 9342 Desc ...

  8. Islands and Bridges(POJ 2288状压dp)

    题意:给你一个图和每个点的价值,边权值为连接两点权值的积,走哈密顿通路,若到达的点和上上个点相连则价值加三点乘积,求哈密顿通路的最大价值,和最大价值哈密顿通路的条数. 分析:开始看这个题很吓人,但想想 ...

  9. poj 1185(状压dp)

    题目链接:http://poj.org/problem?id=1185 思路:状态压缩经典题目,dp[i][j][k]表示第i行状态为j,(i-1)行状态为k时最多可以放置的士兵个数,于是我们可以得到 ...

随机推荐

  1. MySQL中int类型的字段使用like查询方法

    方法参考自: http://stackoverflow.com/questions/8422455/performing-a-like-comparison-on-an-int-field 也就是使用 ...

  2. 设计一个泛型类orderedCollection

    设计一个泛型类orderedCollection,它存储的Comparable对象的集合(在数组中),以及该集合的当前大小.提供public方法isEmpty,makeEmpty,insert,rem ...

  3. php 5.3新增的闭包语法介绍function() use() {}

    * 下面提到的代码在PHP5.3以上版本运行通过. */function callback($callback) { $callback();}//输出: This is a anonymous fu ...

  4. OSX的一些基本知识

    1, 熟悉OSX常用的热键: 花 +C 拷贝(注意,复制是在当前目录中制作一个副本,跟拷贝意义不一样) 花 +V 粘贴(如果用拖拽的方式进行复制,需要按住Option键,相当于Windows的Ctrl ...

  5. 关于面试别问及Spring如何回答思路总结!

    首先要知道 Spring两大核心IOC和AOP(Java轻量级业务层框架Spring两大核心IOC和AOP原理) IOC: 1.从Java最基本的创建对象开始 如Interface Driven De ...

  6. Eclipse 快捷键 (应用中自己总结)

    调试快捷键: 1: resume(F8) 调试中用来直接跳到下一个断点 2:  用来结束JVM 3:step into (F5)跳入函数 4: step over (F6)单步执行 5:step re ...

  7. spring+redis

    配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  8. VMWARE里启动kylin16.0时出现'SMBus Host Controller not enabled'(还未进入系统)

    在Vmware里安装完Ubuntu16.10,启动时出现'SMBus Host Controller not enabled'错误提示,进不到图形界面.网上搜了一下,解决办法是在图形界面里进终端窗口, ...

  9. adb命令学习

    录制屏幕操作Android4.4版本以上支持录制屏幕 adb shell screenrecord /sdcard/demo.mp4 ADB logcat 输出时间信息: adb logcat -v ...

  10. css3动画特效:上下晃动的div

    css3动画特效:上下晃动的div <div id="square" class="container animated">上下晃动</div ...