POJ 2923 - Relocation(状态压缩+01背包)
来源:互联网 发布:linux下cat命令 编辑:程序博客网 时间:2024/05/01 06:51
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 capacitiesC1 andC2 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 capacityC, the sum of the weights of all the furniture it loads for one trip can be at mostC.
Input
The first line contains the number of scenarios. Each scenario consists of one line containing three numbersn,C1 and C2. C1 andC2 are the capacities of the cars (1 ≤Ci ≤ 100) andn is the number of pieces of furniture (1 ≤n ≤ 10). The following line will containn 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
26 12 133 9 13 3 10 117 1 1001 2 33 50 50 67 98
Sample Output
Scenario #1:2Scenario #2:3
题意:
n(1<=n<=10)个物品, 两辆卡车的容量c1, c2. 给出n个物品的体积,两辆卡车同时运作,要求出最少运输多少次能把货物运走。
思路:
状态压缩DP+01背包。
先枚举选择若干个的状态,使用位运算进行枚举。总的状态量为1<<n,判断这些物品能否一次性运走,若能则这一状态看成一个物品。
预处理后得到ii 种状态,在这些状态中使用01 背包,注意判断两个状态不能含有同一种物品,则转化为每一个物品的体积为state[i], 价值为1。
题目转化为求出最小价值,则dp[]初始化为inf。
CODE:
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string>#include <cstring>#include <queue>#include <stack>#include <vector>#include <set>#include <map>const int inf=0xfffffff;typedef long long ll;using namespace std;int c[15], dp[1030], state[1030], vis[1030];int n, c1, c2;bool judge(int x){ int s = 0; memset(vis, false, sizeof(vis)); vis[0] = true; for(int i = 0; i < n; ++i){ if((1<<i) & x){ s += c[i]; for(int j = c1; j >= c[i]; --j){ if(vis[j - c[i]]) vis[j] = true; } } } if(s > c1 + c2) return false; for(int i = 0; i <= c1; ++i){ if(vis[i] && s - i <= c2) { return true; } } return false;}int main(){ //freopen("in", "r", stdin); int T, ca = 0; scanf("%d", &T); while(T--){ ca ++; scanf("%d %d %d", &n, &c1, &c2); for(int i = 0;i < n; ++i){ scanf("%d", &c[i]); } for(int i = 0; i < (1<<n); ++i){ dp[i] = inf; } dp[0] = 0; int ii = 0; for(int i = 1; i < (1<<n); ++i){ if(judge(i)) state[ii++] = i; } for(int i = 0; i < ii; ++i){ for(int j = (1<<n) - 1; j >= 0; --j){ if(dp[j] == inf) continue; if((j & state[i]) == 0){ dp[j | state[i]] = min(dp[j | state[i]], dp[j] + 1); } } } printf("Scenario #%d:\n%d\n\n", ca, dp[(1<<n) - 1]); } return 0;}
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation 状态压缩01背包
- poj 2923 Relocation 状态压缩+01背包
- POJ 2923 - Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+ 01背包)
- poj 2923 Relocation (枚举+背包 | 状态压缩+01背包)
- POJ 2923 Relocation (状态压缩+背包)
- POJ 2923 Relocation(状态压缩+背包思想)
- POJ Relocation 状态压缩 01背包
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation(DP+状态压缩+01背包)
- POJ 2923 Relocation(状态压缩DP+DP:01背包)
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation(状态压缩加上01背包)
- POJ 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation (01背包+状态压缩)
- POJ 2923 Relocation 状态DP+01背包
- eclipse设置java虚拟机内存大小
- 笔试题继续
- 关于编程的64位环境和32位环境
- Android离线语音识别(PocketSphinx)
- DirectX 11游戏编程学习笔记之5: 第4章Direct3D Initialization(Direct3D初始化)
- POJ 2923 - Relocation(状态压缩+01背包)
- Cocos2d-x中Vector<T>容器以及实例介绍
- 第39届ACM亚洲区域赛牡丹江赛区赛后总结
- depcomp: line 571: exec: g++: not found
- GCD nyoj1007(欧拉函数运用&&数论入门)
- 解决ThinkPHP中开启调试模式无法加载模块的问题。
- SQL优化相关--转载
- matlab灰度共生矩阵提取纹理特征
- 好科技好技术