解题报告:POJ_2923 Relocation 状态压缩+01背包
2016-01-14 11:44
357 查看
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 thefurniture 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 numbersn, 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:”, wherei 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
![](http://acm.wust.edu.cn/image/copy.gif)
2 6 12 13 3 9 13 3 10 11 7 1 100 1 2 33 50 70 67 98
Sample Output
Scenario #1: 2 Scenario #2: 3
题意:给你两个背包(c1,c2)和n个物品的重量,问你最次要几次将这些物品全部转移到另一个空间。
类型:状态压缩+01背包
思路:因为n的最大值比较小,考虑到状态压缩+01背包。
先简单介绍一下状态压缩,假设现在有3个物品,则我们有一个位来表示某个物品的状态,1为取,0为不取。则全部取完的状态为(1<<3)-1。1<<3的2进制状态为1000,减去1了后2进制状态为111则分别表示1,2,3,号物品全取,因此第i个物品的状态对应的位便是1<<(i-1)。
�%:6节写在代码里,这里再说一下给数组的含义。
dp
表示运完集合为n的元素需要的最少步数,所以最后的答案就是dp[(1<<n)-1];
state[]记录能一次运完的物品。
代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 1025 int vis ; int m; int dp ; int n,c1,c2; int cost[105]; int state ; bool judge(int x) { memset(vis,0,sizeof(vis)); vis[0]=1;//vis表示该次运输中c1可以装东西的情况,如c1=8,运输的东西体积为3,5,7,则c1背包可以容纳0,3,5,7,8这5种情况,赋1记录。 int sum=0; for(int i=0;i<n;i++) { if((1<<i)&x)//第i个元素在x的集合内 { sum+=cost[i]; for(int j=c1;j>=cost[i];j--) if(vis[j-cost[i]]) vis[j]=1; } } if(sum>c1+c2)return 0; for(int i=0;i<=c1;i++) if(vis[i]&&sum-i<=c2) return 1; return 0; } int main() { int T,t=0; scanf("%d",&T); while(++t<=T) { memset(dp,0x3f,sizeof(dp));//dp初始化为无穷大 dp[0]=0;m=0; scanf("%d%d%d",&n,&c1,&c2); for(int i=0;i<n;i++) scanf("%d",&cost[i]); for(int i=0;i<(1<<n);i++) if(judge(i))//判断i是否能一次运完 state[m++]=i;//渀次能运完的所有情况 for(int i=0;i<m;i++) for(int j=(1<<n)-1;j>=0;j--) { if(dp[j]>1000000)continue;//不可胿剩余的情况跳过 if(!(j&state[i]))//没有重复元素 dp[j|state[i]]=min(dp[j]+1,dp[j|state[i]]); } printf("Scenario #%d:\n%d\n\n",t,dp[(1<<n)-1]); } return 0; }
相关文章推荐
- Android数据保存之PreferenceActivity
- Win10 RS1桌面预览版11099更新:统一核心
- Android 权限大全(整理、更新)
- APP Icon
- JavaScript面向对象之私有静态变量实例分析
- 了解hibernate中的native SQL查询
- 仿大众点评在布局文件中控制listview的显示和隐藏
- linux (redhat) 通过系统镜像安装桌面及开发库
- easyui combobox模糊匹配
- OFbiz--HelloWorld
- css3翻牌效果
- angularjs 判断是否包含 permIDs|filter:'10'
- 3中Mybatis批量查询方式
- Win8已正式退休!是升级Win8.1/Win10还是退回Win7?
- 励志十条
- 激荡20年:IE浏览器的辉煌与落寞
- Android:通知栏的使用
- 西安.NET俱乐部群 推广代码
- C# Best Practices - Creating Good Methods
- 数据用记事本打开导入数据库