比较巧妙的一道题目,拿到题目就想用暴力直接搜索,仔细分析了下发现复杂度达到了2^n*n! ,明显不行,于是只好往背包上想。 于是又想二分找次数判断可行的方法,但是发现复杂度10^8还是很悬。。。
然后学习了这种背包状压的好思路, 巧妙的转化成了背包的模型。 一道经典的题目!
Relocation
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1664   Accepted: 678

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

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

Sample Output

Scenario #1:
2Scenario #2:
3

Source

TUD Programming Contest 2006, Darmstadt, Germany
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;
#define INF 0x3ffffffint c1,c2,n;
int g[11];
int save[10010];
int dp[10010];int check(int s)
{int tg[10];int cnt=0;int sum=0;for(int i=0;i<n;i++){if( ((1<<i)&s) != 0 ){tg[cnt++]=g[i];sum+=g[i];}}for(int i=0;i<(1<<cnt);i++){int tmp,tmp1;tmp=tmp1=0;for(int j=0;j<cnt;j++){if( ((1<<j)&i)!=0){tmp+=tg[j];}}tmp1=sum-tmp;if(tmp<=c1&&tmp1<=c2){return 1;}}// tmp tmp1 分别代表放入的不同地方return 0;
}int main()
{int T;scanf("%d",&T);int tt=1;while(T--){scanf("%d%d%d",&n,&c1,&c2);for(int i=0;i<n;i++)scanf("%d",g+i);int cnt=0;for(int i=1;i<(1<<n);i++)//不能从0开始吧
        {if(check(i)==1){save[cnt++]=i;}}// 求出所有可以当成一次背包的所有情况// 从000000 -> 111111for(int i=1;i<(1<<n);i++)dp[i]=INF;dp[0]=0;for(int i=0;i<cnt;i++){for(int j=(1<<n);j>=save[i];j--){if( (j| save[i] ) != j) continue;dp[j]=min(dp[j],dp[j^save[i]]+1);}}printf("Scenario #%d:\n",tt++);printf("%d\n",dp[(1<<n)-1]);printf("\n");}return 0;
}

poj 2923(状态压缩+背包)相关推荐

  1. poj 2923(状态压缩dp)

    题意:就是给了你一些货物的重量,然后给了两辆车一次的载重,让你求出最少的运输次数. 分析:首先要从一辆车入手,搜出所有的一次能够运的所有状态,然后把两辆车的状态进行合并,最后就是解决了,有两种方法: ...

  2. TYVJ 2054 [Nescafé29]四叶草魔杖 最小生成树 状态压缩/背包DP

    $ \rightarrow $ 戳我进TYVJ原题 [Nescafé29]四叶草魔杖 题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背 ...

  3. poj 3797(状态压缩dp)

    题意:4*n的木板,用1*2方块去贴,问一共有多少种方案. 解题思路:这道题是简单的状态压缩,和之前的铺方块是一样的思路,横着的为全1,如果有空格等着下一行去铺就置0,那么下一行的这个位置肯定为1,因 ...

  4. POJ 3254 状态压缩DP

    我的状态压缩的第一题,忘记取模错了2次. 符合的状态000 001 010 100 101  记得和该行原来状态看是否符合. #include<cstdio> #include<io ...

  5. poj 2288(状态压缩dp + TSP问题)

    题目描述:哈密尔顿路问题.n个点,每一个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算: 1.每一个点的权值之和 2.对于图中的每一条CiCi+1,加 ...

  6. POJ 2411 Mondriaan‘s Dream(最清楚好懂的状压DP讲解)(连通性状态压缩DP)

    poj 2411 Mondriaan's Dream(最清晰的状压DP解析) 闫氏DP大法好 我们这里是一列一列地来,因为是一个棋盘性的状态压缩DP,从哪个方向都一样 摆放的小方格总方案数 等价于 横 ...

  7. POJ 1185 炮兵阵地(状态压缩DP)

    Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用&quo ...

  8. 【hdu4281状态压缩+01背包+多旅行商问题】

    题意:有n道题,每道题在二维平面内的不同位置且给出每道题的坐标,同时给出处理每道题所需的时间p,现在已知裁判的耐心有限,之会花费m个单位时间去做事,做完后回到起始点. 现在的要求是,我要多少个裁判才能 ...

  9. POJ - 3279 Fliptile(状态压缩+位运算+暴力)

    题目链接:点击查看 题目大意:给出一个n*m的01矩阵,为了好描述,我们设0和1是两个相反的状态,我们的目标是要将整个矩阵全部变成1,现在我们可以将某一个点(x,y)更改为相反的状态,不过相应的该点周 ...

最新文章

  1. 嵌入式linux 升级,嵌入式Linux的OTA更新,基本原理和实现
  2. python跨行字符串 变量_在Python中有没有在多行字符串中使用变量的方法?
  3. [再寄小读者之数学篇](2014-04-18 from 352558840@qq.com [南开大学 2014 年高等代数考研试题]二次型的零点)...
  4. 51单片机C语言波特率十六进制,理解51单片机串口通信的波特率与USB转串口通信...
  5. document 文挡对象 - JavaScript脚本语言描述
  6. redux provider源码解析
  7. Jquery ajax 访问调用带参数的服务方法!
  8. pip与conda简述
  9. 一个小例子介绍Obj-C的函数命名方式
  10. getchar与getch函数的区别
  11. WPS Office 2016 专业版终身授权正版
  12. java从键盘输入若干学生的成绩_初学Java3:数组-从键盘录入若干学生的成绩,计算平均值,最大值,最小值...
  13. 你和财务自由之间,只差一个公式
  14. Spring MVC学习(6)—Spring数据类型转换机制全解【一万字】
  15. 基于图神经网络的时空预测
  16. Linux中vsftpd服务配置(匿名,用户,虚拟用户)
  17. GnomeSort(侏儒排序)——C语言实现
  18. CSMA/CD技术详解
  19. IDEA中的单元测试模块(Junit)不能自动导包
  20. MFC加载GIF动画

热门文章

  1. C/C++ 实现命令行画心形代码
  2. Exposure X7胶片滤镜调色插件免费版下载
  3. HCIE认证培训 华为AAA认证详解-ielab
  4. 阿里百川SDK集成注意点
  5. JavaScript实现随机抽奖代码
  6. 敏捷转型如何抓住人心:钱多事少离家近?
  7. Debian Jessie 开启软键盘
  8. 全国高速公路编号对照表
  9. 渗透测试学习笔记-Nmap使用
  10. 《拆掉思维里的墙》 古典 (初读此书,相见恨晚!)