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

来源:互联网 发布:见过大业观后感知乎 编辑:程序博客网 时间:2024/05/01 14:06

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 mostC.

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

26 12 133 9 13 3 10 117 1 1001 2 33 50 50 67 98

Sample Output

Scenario #1:2Scenario #2:

3

题意:

两辆车运送n个货物,每个车有最大容积maxv1和maxv2,求最少运送次数。

解题思路:

状态压缩,把所有能够一次性(2辆车)运走的物品状态求出来,放到数组C中。

数组C中的每一个状态就对应了一个物品,耗费是1次,容量是该状态装载的物品。

进行01背包,与普通背包不同,普通背包的二层循环中的变量是V,此处变量是装载物品的状态(从0到1<<n)。

答案就是dp[(1<<n)-1](滚动数组)。

详见代码注释。

代码:

#include<cstdio>#include<iostream>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int dp[2400],c[2400],a[15],vis[1005];int n,maxv1,maxv2;int ok(int p){        int sum=0;        memset(vis,0,sizeof vis);        vis[0]=1;        for(int i=0;i<n;i++)                if(p&(1<<i))  //第i个物品在该状态中                {                        sum+=a[i];                        for(int j=maxv1;j>=a[i];j--)                                if(vis[j-a[i]])     //标记该状态下装载物品的所能组合的所有状态(的总体积)<span style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; white-space: pre-wrap;">                          </span>
                }        if(sum>maxv1+maxv2)         //超载                return 0;        for(int i=0;i<=maxv1;i++)        {                if(vis[i]&&sum-i<=maxv2)      //存在这种分法(i和sum-i)且不超过两辆车各自的体积                        return 1;        }        return 0;}int main(){        int T;        int w=0;        cin>>T;        while(T--)        {                int cnt=0;                cin>>n>>maxv1>>maxv2;                for(int i=0;i<n;i++)                        cin>>a[i];                for(int i=0;i<(1<<n);i++)         //遍历所有状态                {                        dp[i]=INF;                        if(ok(i))                                c[cnt++]=i;        //能够一次运走的状态放入C中                }                dp[0]=0;                            //不运走任何物品需要0次                for(int i=0;i<cnt;i++)                {                        for(int j=(1<<n)-1;j>=0;j--)                        {                                if(dp[j]==INF)<span style="white-space:pre">//减枝                                        continue;                                if((j&c[i])==0)           //当前背包中装的东西与c[i]中的东西没有重复                                {                                        if(dp[j|c[i]]>dp[j]+1)                                        dp[j|c[i]]=dp[j]+1;                                }                        }                }                printf("Scenario #%d:\n%d\n",++w, dp[(1<<n)-1] );                if(T!=0)                        cout<<endl;        }}


0 0
原创粉丝点击