解题报告:POJ_2923 Relocation 状态压缩+01背包

来源:互联网 发布:淘宝店铺企业店铺 编辑:程序博客网 时间:2024/05/16 11:01

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 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 andC2.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 integersw1, …,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 70 67 98

Sample Output

Scenario #1:2Scenario #2:3



题意:给你两个背包(c1,c2)和n个物品的重量,问你最次要几次将这些物品全部转移到另一个空间。

 

 

思路:因为n比较小,考虑到状态压缩+01背包。

先简单介绍一下状态压缩,假设现在有3个物品,则我们有一个位来表示某个物品的状态,1为取,0为不取。则全部取完的状态为(1<<3)-1。1<<3的2进制状态为1000,减去1了后2进制状态为111则分别表示1,2,3,号物品全取,因此第i个物品的状态对应的位便是1<<(i-1)。


先扫一遍所有的集合,从中选出能一次运完的集合,然后dp一下就好了,递推公式:dp[j|state[i]]=min(dp[j]+1,dp[j|state[i]]);


dp[n]表示运完集合为n的元素需要的最少步数,所以最后的答案就是dp[(1<<n)-1];

state[]记录能一次运完的集合。



代码:


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 1025int n,m,c1,c2;int dp[N];int vis[N];int cost[105];int state[N];bool judge(int x)//x为状压后的一个集合{    memset(vis,0,sizeof(vis));    vis[0]=1;//vis为第一个背包能装的情况    int sum=0;//sum记录集合内消耗空间的总值    for(int i=0;i<n;i++){        if((1<<i)&x){//如果第i个元素在x的集合内            sum+=cost[i];            for(int j=c1;j>=cost[i];j--){//01背包                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){//如果第一个背包装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;}




0 0
原创粉丝点击