poj2923 Relocation (01背包+状态压缩)
来源:互联网 发布:生死狙击破密保软件 编辑:程序博客网 时间:2024/05/15 07:58
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:
- At their old place, they will put furniture on both cars.
- Then, they will drive to their new place with the two cars and carry the furniture upstairs.
- 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 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
26 12 133 9 13 3 10 117 1 1001 2 33 50 50 67 98
Sample Output
Scenario #1:2Scenario #2:3
Source
第6题,这道题是个好题,n<10可以用状态压缩表示所有情况,先用01背包预处理所有能一次运走的情况,然后再一次01取最小的运输次数,虽然复杂,想清楚了其实很简单
#include <iostream>#include <cstring>#include <cstdio>#define inf 0x3f3f3f3fusing namespace std;int w[11],f[1<<10],g[1<<10],v[105];int n,c1,c2,t;int work01(int p){ int sum=0; memset(v,0,sizeof(v)); v[0]=1; for(int i=0;i<n;i++){ if(p&(1<<i)){ sum+=w[i]; for(int j=c1-w[i];j>=0;j--) if(v[j])v[j+w[i]]=1; } } for(int i=c1;i>=0;i--){ if(v[i]&&sum-i<=c2)return 1; } return 0;}int main(){ int cas=1; scanf("%d",&t); while(t--){ int s=0; scanf("%d%d%d",&n,&c1,&c2); for(int i=0;i<n;i++)scanf("%d",&w[i]); for(int i=1;i<(1<<n);i++){ if(work01(i))g[s++]=i; } memset(f,inf,sizeof(f)); f[0]=0; for(int i=0;i<s;i++) for(int j=(1<<n)-1-g[i];j>=0;j--) if(!(j&g[i]))f[j|g[i]]=min(f[j|g[i]],f[j]+1); printf("Scenario #%d:\n%d\n\n",cas++,f[(1<<n)-1]); } return 0;}
- poj2923 Relocation (01背包+状态压缩)
- poj2923 Relocation(状态压缩+01背包)
- POJ2923:Relocation(状态压缩+01)
- poj2923 Relocation(状态压缩+背包)
- poj2923 Relocation(压缩dp+01背包)
- poj2923 Relocation(枚举+01背包)
- poj2923(状态压缩+01背包)
- poj2923 01背包+状态压缩dp
- POJ2923:Relocation(状态压缩dp)
- poj2923 状态压缩背包dp
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation 状态压缩01背包
- poj 2923 Relocation 状态压缩+01背包
- POJ 2923 - Relocation(状态压缩+01背包)
- POJ Relocation 状态压缩 01背包
- POJ 2923 Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+ 01背包)
- poj 2923 Relocation (枚举+背包 | 状态压缩+01背包)
- 关于使用Robotium进行Android测试的一点小改进
- 01 jquery基础
- 解决Myeclipse在调试(debug)时无法显示变量值问题
- MAVEN 打包时出现多余的类的问题
- HTTP协议头字段(header fields)索引
- poj2923 Relocation (01背包+状态压缩)
- 字典树
- Eclipse 调试Android Framework C篇
- FFMPEG AV sync
- 尽我垫VPN为中国浏览互联网安全
- SVN+Jenkins+Maven+Appium+TestNG+ReportNG 实战 Android 自动化测试
- Beetl 自定义标签的使用笔记
- 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclips
- Python 不同对象比较大小