POJ--2923--Relocation--如压力DP

Relocation

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:

  1. At their old place, they will put furniture on both cars.
  2. Then, they will drive to their new place with the two cars and carry the furniture upstairs.
  3. 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 nC1 and C2C1 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的学习。我还刚接触,所以不会没有节操地大发厥词来写算法总结,嘎嘎嘎,前两天说。

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-08-30 11:49:59

POJ--2923--Relocation--如压力DP的相关文章

poj 2923 Relocation 解题报告

题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物品状态:0表示没运走,1表示已被运走. 枚举出两辆车一趟可以运出的状态.由于物品是一趟一趟运出来的.所以就可以由一个状态通过两辆车一趟的状态转移到另一个状态. dp[i]=MIN(dp[k]+1).k可以由两车一趟转移到i. 我是参考此人的:http://blog.csdn.net/bossup/a

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

题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开,问最少需要搬运几次? 我先想的是我由A车开始搬,搬运能装的最大的家具总重,然后状态压缩记录下搬运了哪些,然后再由B车搬运,状态压缩记录搬运了哪些.以此类推,直到状态满了. 以上方法TLE 然后,实在想不出来了,看了题解:http://blog.csdn.net/woshi250hua/articl

POJ 2923 Relocation(状压+背包)

Relocation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2340   Accepted: 965 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 r

POJ 2923 Relocation

题目大意:有n个物品,有两辆车载重分别是c1,c2.问需要多少趟能把物品运完. (1 ≤ Ci ≤ 100,1 ≤ n ≤ 10,1 ≤ wi ≤ 100). 题解:n小思状压.我们先把所有一次可以拉走的状态ini预处理好,然后把这些状态当做一个个物品跑背包就行了. 合并的转移:dp[j|ini[i]]=min(dp[j|ini[i]],dp[j]+1); 就是如果状态j与ini[i]不冲突,则可以从状态j运一趟变为j|ok[i]. 然后正确性可以YY一下:为什么可以看成是物品?并一下就可以?是

Relocation POJ - 2923(01背包+状压dp)

Relocation POJ - 2923 原文地址:https://www.cnblogs.com/megadeth/p/11361007.html

状态压缩DP——POJ 2923

对应POJ题目:点击打开链接 Exponentiation Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2923 Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortu

POJ 1692 Crossed Matchings(DP)

Description There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with values r, if one of them is located in the first row and the other one is located in the second row. We call this line segmen

POJ 3356 AGTC 最短编辑距离 DP

http://poj.org/problem?id=3356 题意: 给两个长度不大于1000的串,修改其中一个串使得两串相同,问最少修改次数.修改有三种,插入一个字符,删除一个字符,改变一个字符. 分析: 直接给方程. dp[i][j]表示第一个串前i位和第二串前j位匹配的最小修改次数. dp[0][0] = 0, dp[length(x)][length(y)]为答案. dp[i][j] = min(dp[i-1][j-1] + x[i] != y[j], dp[i-1][j] + 1, d

POJ 3666 Making the Grade [DP]

题意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,这里的代价看题目公式. 思路: 很容易想到是DP. 1. 对前i个序列,构成的最优解其实就是与两个参数有关.一个是这个序列处理后的最大值mx,和这个序列处理的代价值cost. 显然最大值mx最小最好(这样第i+1个值可以不花代价直接接在其后面的可能性更大),cost最小也最好(题意要求),但是两者往往是鱼和熊掌. 用dp[i][j]表示:前i个数构成的序列,这个序列最大值为j,dp[i][j]的值代表相应的cost. 所以状态转移方

poj 2081 Recaman&#39;s Sequence (dp)

Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 22566   Accepted: 9697 Description The Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence,