poj 2923(状态压缩+背包)
Relocation
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:
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 n, C1 and C2. C1 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(状态压缩+背包)相关推荐
- poj 2923(状态压缩dp)
题意:就是给了你一些货物的重量,然后给了两辆车一次的载重,让你求出最少的运输次数. 分析:首先要从一辆车入手,搜出所有的一次能够运的所有状态,然后把两辆车的状态进行合并,最后就是解决了,有两种方法: ...
- TYVJ 2054 [Nescafé29]四叶草魔杖 最小生成树 状态压缩/背包DP
$ \rightarrow $ 戳我进TYVJ原题 [Nescafé29]四叶草魔杖 题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背 ...
- poj 3797(状态压缩dp)
题意:4*n的木板,用1*2方块去贴,问一共有多少种方案. 解题思路:这道题是简单的状态压缩,和之前的铺方块是一样的思路,横着的为全1,如果有空格等着下一行去铺就置0,那么下一行的这个位置肯定为1,因 ...
- POJ 3254 状态压缩DP
我的状态压缩的第一题,忘记取模错了2次. 符合的状态000 001 010 100 101 记得和该行原来状态看是否符合. #include<cstdio> #include<io ...
- poj 2288(状态压缩dp + TSP问题)
题目描述:哈密尔顿路问题.n个点,每一个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算: 1.每一个点的权值之和 2.对于图中的每一条CiCi+1,加 ...
- POJ 2411 Mondriaan‘s Dream(最清楚好懂的状压DP讲解)(连通性状态压缩DP)
poj 2411 Mondriaan's Dream(最清晰的状压DP解析) 闫氏DP大法好 我们这里是一列一列地来,因为是一个棋盘性的状态压缩DP,从哪个方向都一样 摆放的小方格总方案数 等价于 横 ...
- POJ 1185 炮兵阵地(状态压缩DP)
Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用&quo ...
- 【hdu4281状态压缩+01背包+多旅行商问题】
题意:有n道题,每道题在二维平面内的不同位置且给出每道题的坐标,同时给出处理每道题所需的时间p,现在已知裁判的耐心有限,之会花费m个单位时间去做事,做完后回到起始点. 现在的要求是,我要多少个裁判才能 ...
- POJ - 3279 Fliptile(状态压缩+位运算+暴力)
题目链接:点击查看 题目大意:给出一个n*m的01矩阵,为了好描述,我们设0和1是两个相反的状态,我们的目标是要将整个矩阵全部变成1,现在我们可以将某一个点(x,y)更改为相反的状态,不过相应的该点周 ...
最新文章
- 嵌入式linux 升级,嵌入式Linux的OTA更新,基本原理和实现
- python跨行字符串 变量_在Python中有没有在多行字符串中使用变量的方法?
- [再寄小读者之数学篇](2014-04-18 from 352558840@qq.com [南开大学 2014 年高等代数考研试题]二次型的零点)...
- 51单片机C语言波特率十六进制,理解51单片机串口通信的波特率与USB转串口通信...
- document 文挡对象 - JavaScript脚本语言描述
- redux provider源码解析
- Jquery ajax 访问调用带参数的服务方法!
- pip与conda简述
- 一个小例子介绍Obj-C的函数命名方式
- getchar与getch函数的区别
- WPS Office 2016 专业版终身授权正版
- java从键盘输入若干学生的成绩_初学Java3:数组-从键盘录入若干学生的成绩,计算平均值,最大值,最小值...
- 你和财务自由之间,只差一个公式
- Spring MVC学习(6)—Spring数据类型转换机制全解【一万字】
- 基于图神经网络的时空预测
- Linux中vsftpd服务配置(匿名,用户,虚拟用户)
- GnomeSort(侏儒排序)——C语言实现
- CSMA/CD技术详解
- IDEA中的单元测试模块(Junit)不能自动导包
- MFC加载GIF动画