POJ--2923--Relocation--如压力DP
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2288 | Accepted: 950 |
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
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个物品,两个车,车有自己的容量,物品有自己的体积。求最少的运送次数把物品所有运走,注意,两车一同发车。
解析:状态压缩DP,列举两车可以装下物品的全部方案。然后二进制找两车没有运同样物品的方案,然后用这些方案进行推算就出来了。
注意了,状态压缩就是用二进制的每一位来替代每一个物品,所以n个物品的话最大就是n位
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max (1<<15)
using namespace std;
int main (void)
{
int t,n,c1,c2,i,j,k,l1,l2,L,cas=1;
int s[11],dp[Max],s1[Max],s2[Max],dis[Max];
scanf("%d",&t);
while(t--&&scanf("%d%d%d",&n,&c1,&c2))
{
for(i=0;i<n;i++)
scanf("%d",&s[i]);
l1=l2=0;
for(i=0;i<(1<<n);i++) //演算全部可能方案
{
k=0;
for(j=0;j<n;j++) //拿这个方法计算出方案中全部物品的值的总和
if(i&(1<<j))
k+=s[j];
if(k<=c1)s1[l1++]=i; //总和小于c1车的容量表示这个方法能够用c1车来实现
if(k<=c2)s2[l2++]=i; //同上
}
L=0;
for(i=0;i<l1;i++) //一一相应比較
for(j=0;j<l2;j++)
if((s1[i]&s2[j])==0) //与运算用来推断两个二进制数是否有某些位同样,即推断是否某些物品被两车都装了
dis[L++]=(s1[i]|s2[j]);
memset(dp,-1,sizeof(dp));
dp[0]=0; //基础方案初始化
for(i=0;i<(1<<n);i++) //遍历全部状态
if(dp[i]>-1) //预算的前提是这个基础点有值
{
for(j=0;j<L;j++) //遍历全部方案
{
if((i&dis[j])==0&&(dp[i|dis[j]]==-1||dp[i|dis[j]]>dp[i]+1))//这里i&dis[j]==0是用来确定当前状态与当前方案没有冲突。冲突是指的当前状态已经用过某物品而这个方法正好要使用这个物品
dp[i|dis[j]]=dp[i]+1;
}
}
printf("Scenario #%d:\n%d\n\n",cas++,dp[(1<<n)-1]);//输出全部物品都被运送完了的状态中记录的值
}
return 0;
}
我认为这个题非常适用于状压DP的学习。我还刚接触,所以不会没有节操地大发厥词来写算法总结,嘎嘎嘎,前两天说。
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ--2923--Relocation--如压力DP的更多相关文章
- POJ 2923 Relocation 装车问题 【状态压缩DP】+【01背包】
题目链接:https://vjudge.net/contest/103424#problem/I 转载于:>>>大牛博客 题目大意: 有 n 个货物,并且知道了每个货物的重量,每次用 ...
- POJ 2923 Relocation(01背包变形, 状态压缩DP)
Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...
- POJ 2923 Relocation(状压DP+01背包)题解
题意:给你汽车容积c1,c2,再给你n个包裹的体积,问你最少运几次能全运走 思路:用2进制表示每次运送时某物在不在此次运送之中,1在0不在.我们把运送次数抽象成物品价值,把状态抽象成体积,用一个dp[ ...
- POJ 2923 Relocation(状压DP)题解
题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...
- [POJ 2923] Relocation (动态规划 状态压缩)
题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...
- poj 2923 Relocation 解题报告
题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物 ...
- POJ 2923 Relocation(01背包+状态压缩)
题意:有人要搬家,有两辆车可以运送,有若干家具,车有容量限制,而家具也有体积,那么如何运送会使得运送车次最少?规定两车必须一起走,两车一次来回只算1躺. 思路:家具怎么挑的问题,每趟车有两种可能:1带 ...
- POJ 2923 Relocation (状态压缩,01背包)
题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...
- POJ 2923 Relocation
题目大意:有n个物品,有两辆车载重分别是c1,c2.问需要多少趟能把物品运完. (1 ≤ Ci ≤ 100,1 ≤ n ≤ 10,1 ≤ wi ≤ 100). 题解:n小思状压.我们先把所有一次可以拉 ...
- HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...
随机推荐
- nginx & flup & django & python3.x @ window7配置备忘录
最近考虑原Prism建筑(非职业.半专业人士认为C/S建筑)至B/S迁移,主要是由于部署问题,包括两个因素:已经做,虽然一键安装和部署的一个因素,心存顾虑,虽然我一再声明这是一个绿色软件.还有一个因素 ...
- UVA - 11388 GCD LCM
II U C ONLINE C ON TEST Problem D: GCD LCM Input: standard input Output: standard output The GC ...
- SQL Server定时自动抓取耗时SQL并归档数据脚本分享
原文:SQL Server定时自动抓取耗时SQL并归档数据脚本分享 SQL Server定时自动抓取耗时SQL并归档数据脚本分享 第一步建库 USE [master] GO CREATE DATABA ...
- RFC2889转发性能測试用例设计和自己主动化脚本实现
一.203_TC_FrameRate-1.tcl set chassisAddr 10.132.238.190 set islot 1 set portList {9 10} ;#端口的排列顺序是po ...
- window应用移植到Linux下(应用移植)
配置QT的环境变量,这台电脑à属性à高级系统设置à高级à环境变量à系统变量àpathàC:\Qt\Qt5.3.0\5.3\mingw482_32\bin;C:\Qt\Qt5.3.0\Tools\ ...
- Oracle Cursor的使用
When Oracle Database executes a SQL statement, it stores the result set and processing information i ...
- ipv6加英文的中括号访问
加英文的中括号就可以,如[2001:4998:c:e33::1004],我发现这是yahoo首页.但并不是所有IPv6网站都可以通过IPv6地址访问,跟IPv4一样,网站服务器端可以只绑定域名,不接受 ...
- Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...
- SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge (并不在同一个版本库中)
在使用svn merge命令报错 英文版本:SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge 中文版本报错:并不 ...
- Android SDK 5.0 这个语句带来折腾 - 生命在于折腾!
Android SDK 5.0 带来的这番折腾 - 生命在于折腾! 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一 ...