题目链接

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
2
6 12 13
3 9 13 3 10 11
7 1 100
1 2 33 50 50 67 98
Sample Output
Scenario #1:
2 Scenario #2:
3

【题意】:两辆车n个物品,每个物品有体积,两辆车也有体积,要求把物品全部运走最少需要多少次,每次每辆车运送的物体总体积不得大于车的体积。

【分析】:

1.掌握位运算的运算法优先级别很重要

2.掌握基本位运算

(1).判断是否为0 if((S&1<<i)==0)

(2).将第i位置1 S|1<<i

(3).将第i位置0 S&~(1<<i)

3.要有状态的思想 每次是对状态进行操作


预处理所有合法组合...

从合法组合中选取最小的次数。


#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5 + 5;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/*
总的复杂度是O(VNK)
3
5 10 2
1 2 3 4 5
5 4 3 2 1
5 10 12
1 2 3 4 5
5 4 3 2 1
5 10 16
1 2 3 4 5
5 4 3 2 1
*/
int n,m,K;
#define S 100000
#define M 200000
int state[1030];
int dp[1030];
bool vis[1005];
int v1,v2,tot;
int c[12];
bool ok(int x)//判断一种状态是否可行(可以一次运走)
{
int sum = 0;
ms(vis,0);
vis[0] = 1;
for (int i = 0;i < n;i++)
{
if ((x>>i)&1)
{
sum += c[i];
for (int j = v1;j >= c[i];j--) //这个真的非常巧妙 开始看半天都不懂,自己模拟一遍才懂
{ //比如说此状态有c1、c2、c3,3个体积,第一次操作把体积c1标记为1,
if (vis[j-c[i]]) //第二次操作把c2和c1+c2两种体积标记为1,第三次把c3和前面的组合标记为1,
vis[j] = 1; //最后这些体积能组合成的所有体积就都被标记成了1
}
}
}
if (sum > v1+v2 )//装不下
return 0;
//总体积小不代表一定装得下,拆分成2份要2份都装得下
for (int i = 0;i <= v1;i++)
{
if (vis[i] && sum-i <= v2)//如果存在(i,sun-i)这样的组合
return 1; //满足i可以被v1装下(前面for循环是对于v1的,vis[i]表示体积i可以被v1装下),sun-i可以被v2装下
}
return 0;
}
void init()//初始化找到满足条件的状态
{
tot = 0;
for (int i = 1; i < (1<<n); i++)
{
dp[i] = INF;
if (ok(i))
state[tot++] = i;
}
}
int main()
{
int T;
cin>>T;
int oo = 0;
while (T--)
{
cin>>n>>v1>>v2;
for (int i = 0;i < n;i++)
scanf("%d",&c[i]);
init();
int V = (1<<n) - 1;//V是n个1的二进制数
dp[0] = 0; //没有物品当然是0次运走
for (int i = 0;i < tot;i++)
{
for (int j = V;j >= 0;j--)
{
if (dp[j] == INF)
continue; //原版的背包是dp[j] = min(dp[j],dp[j-c[i]]+w[i])
//但是显然二进制不好表示减,但是可以用|抽象加
//这就相当于背包改版成dp[j+c[i]] = min(dp[j+c[i]],dp[j] + w[i])
if ((j&state[i])==0) //当然2种状态不能有交集
{
dp[j|state[i]] = min(dp[j|state[i]] ,dp[j] + 1);
}
}
}
printf("Scenario #%d:\n%d\n",++oo,dp[V]);
if (T) puts("");
}
return 0;
}
// dp[j|s[i]] = min(dp[j|s[i]], dp[j]+1);//在运输了状态j上,在运输s[i], 变成状态j|s[i]
//如果dp[i-1][j-w[i]]已经出现了(就是说如果存在了重量j-w[i],我只需要填上重量w[i]就可以形成j),那么dp[i-1][j]就可以组成。
//如果满足q[i]这种组合的搬运,就需要增加一次搬运dp[j]+1
//然后两个用或运算合起来,表示一次两车运的物体。(注意:要把载最重的车能载的情况也看作是一次合)
////vis为第一个背包能装的情况 //x为状压后的一个集合 //在运输了状态j上,在运输s[i], 变成状态j|s[i]
////01背包因为减法不好用2进制表示,因此选择加法的DP

POJ 2923 【01背包+状态压缩/状压DP】的更多相关文章

  1. POJ 1321 棋盘问题(DFS & 状压DP)

    用DFS写当然很简单了,8!的复杂度,16MS搞定. 在Discuss里看到有同学用状态压缩DP来写,就学习了一下,果然很精妙呀. 状态转移分两种,当前行不加棋子,和加棋子.dp[i][j]中,i代表 ...

  2. POJ:1185-炮兵阵地(状压dp入门)

    炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组 ...

  3. POJ 3311 Hie with the Pie (状压DP)

    dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...

  4. poj 2404 中国邮递员问题 欧拉回路判定+状压dp

    /* 状压dp 邮递员问题:求经过任意点出发经过每一条边一次并回到原点. 解法:1.如果是欧拉回路那么就是所有的边的总和. 2.一般的解法,找出所有的奇度顶点,任意两个顶点匹配,即最小完美匹配,可用状 ...

  5. POJ 2923 Relocation (状态压缩,01背包)

    题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...

  6. POJ 2923 Relocation(01背包+状态压缩)

    题意:有人要搬家,有两辆车可以运送,有若干家具,车有容量限制,而家具也有体积,那么如何运送会使得运送车次最少?规定两车必须一起走,两车一次来回只算1躺. 思路:家具怎么挑的问题,每趟车有两种可能:1带 ...

  7. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  8. POJ 2411 Mondriaan's Dream 【状压Dp】 By cellur925

    题目传送门 这道题暑假做的时候太模糊了,以前的那篇题解大家就别看了==.今天再复习状压感觉自己当时在写些什么鸭.... 题目大意:给你一个\(n\)*\(m\)的棋盘和许多\(1*2\)的骨牌,骨牌可 ...

  9. 【POJ】1185 炮兵阵地(状压dp)

    题目 传送门:QWQ 分析 看到$ M<=10 $考虑状压. 然后把每行都压一下,那么每个状态相关的就是上一行和上上行的状态. 然后枚举. 然后复杂度最坏是$ O(100 \times 1024 ...

随机推荐

  1. 随笔 —— 门徒 & 无限恐怖

    门徒 忧思缠身,所为何物 不知何人,可免世俗 每每朝暮,心无释处 悲从中来,如泣如诉 仁者存世,满怀悲苦 逝者如斯,追还无路 上天无门,开怀捧腹 无路偏行,我行我素 无限恐怖 饥寒苦难谁知故,日日行路 ...

  2. vue.js中created方法作用

    这是它的一个生命周期钩子函数,就是一个vue实例被生成后调用这个函数.一个vue实例被生成后还要绑定到某个html元素上,之后还要进行编译,然后再插入到document中.每一个阶段都会有一个钩子函数 ...

  3. save?commit

    数据库的隐式提交 先看一段SQL,最后一条SQL的输出你认为是什么? 1 2 3 4 5 6 7 SET AUTOCOMMIT = 1; BEGIN; INSERT INTO t1 VALUES (1 ...

  4. time模块与random模块,六位含字母随机验证码

    # time模块# import time# time.time()#计算这一时刻时间戳 *******# time.sleep(1)#让cpu休眠一定时间 *******# time.clock() ...

  5. docker部署思路

    1.docker安装2.拉取centos镜像或者Ubuntu镜像 看你用哪个3.使用镜像,run出来一个容器A4.进入容器A,安装uwsgi,把Django部署在下面5.在启动脚本中配置开机自启动脚本 ...

  6. QQ网页强制聊天,微博一键关注

    <!doctype html> <!-- 微博关注需要的js --> <html xmlns:wb="http://open.weibo.com/wb" ...

  7. 转:sift算法详解

    转自:http://blog.csdn.net/pi9nc/article/details/23302075 对于初学者,从David G.Lowe的论文到实现,有许多鸿沟,本文帮你跨越. 1.SIF ...

  8. HDU 4747 Mex ( 线段树好题 + 思路 )

    参考:http://www.cnblogs.com/oyking/p/3323306.html 相当不错的思路,膜拜之~ 个人理解改日补充. #include <cstdio> #incl ...

  9. HDU 2295 Radar (二分 + Dancing Links 重复覆盖模型 )

    以下转自 这里 : 最小支配集问题:二分枚举最小距离,判断可行性.可行性即重复覆盖模型,DLX解之. A*的启发函数: 对当前矩阵来说,选择一个未被控制的列,很明显该列最少需要1个行来控制,所以ans ...

  10. opencv3.1+cmake+mingw5.3+QT5编译

    太不容易了! 想要访问opencv的官网貌似要FQ才行.下载了opencv3.2版本,发现cmake在download opencv_ffmpeg.dll的地方超时了. 于是搜索一番,发现很多编译op ...