POJ __2923 Relocation

来源:互联网 发布:大学生炒股知乎 编辑:程序博客网 时间:2024/06/15 07:52

题目描述;

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.

你有两辆卡车和n个物品,每个物品有自己的重量,你需要多少次搬运才能将物品搬完。

输入T,测试组数,然后n,n个物品,C1,C2两辆卡车的载重,下一行n个物品的重量。n≤10;

样例:
input:

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

output:

Scenario #1:2Scenario #2:3
但看这道题看起来很复杂,物品和卡车的选取情况太多,但其实数据范围给了一个非常重要的提示,n最多只有10,也就是说物品非常少,那么我们就可以用状态压缩的方式将这些复杂的情况的简化。
卡车的数量一定,那么我们将物品的选取情况用二进制数进行编码,举个例子:比如我有三个物品,那么110就代表我选择了第二个和第三个物品,001代表我只选择了第一个物品,所以111就代表我三个物品都选了,所以物品选取的状态总数也就是(1<<n)-1,就是2^n-1;
解决了编码问题后,我们还要处理的是这些状态是否都可行,因为每次两辆卡车的载重是固定且有限的,所以必须要保证我们每次选取的物品可以被分装在两辆卡车上。
我们采取的编码方式决定了二进制数每一个为1的位就是我们该状态下选取的物品,那么我们每次将该状态x向右移动i位,i从0到n,也就是(x>>i)&1如果为1说明该位上有1,也就是第i个物品被选取了,同时用一个标记数组vis来记录该重量是否存在,这个就可以用于判断该状态是否可行,具体的代码中可以看清楚。
处理完之后我们将所有可行的状态存进state数组里,将这每一种状态单独看成一个物品的话,其实这个题就是一个简单的背包模型,直接DP即可,DP时注意一下物品是不能被重复选的,所以这个地方要判断一下;
最后提醒的是,状态压缩一定要想清楚各种二进制运算的结果和目的。
AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<string>#include<stack>#include<queue>#include<algorithm>#include"stdlib.h"using namespace std;const int MAXM=1010;const int INF=999999999;int n,C1,C2;int w[2010];int dp[2010];int state[2010];bool vis[2010];void init(){    for (int i=0;i<(1<<n);i++)        dp[i]=INF;}bool check(int x){    int sum=0;    memset(vis,false,sizeof(vis));    vis[0]=true;    for (int i=0;i<n;i++)    {        if ((x>>i)&1)        {            sum+=w[i];            for (int j=C1;j>=w[i];j--)                if (vis[j-w[i]])                vis[j]=true;        }    }    if (sum>C1+C2)        return false;    for (int j=0;j<=C1;j++)        if (vis[j]&&sum-j<=C2)        return true;    return false;}int main(){    int T;    scanf("%d",&T);    int cas=1;    while(T--)    {        scanf("%d%d%d",&n,&C1,&C2);        init();        for (int i=0;i<n;i++)            scanf("%d",&w[i]);        int tot=0;        for (int i=0;i<(1<<n);i++)            if (check(i))            state[tot++]=i;        dp[0]=0;        for (int i=0;i<tot;i++)        {            for (int j=(1<<n)-1;j>=0;j--)            {                if (j&state[i]) continue;//判断物品是否被重复选取                if (dp[j]==INF) continue;                dp[j|state[i]]=min(dp[j|state[i]],dp[j]+1);            }        }        printf("Scenario #%d:\n",cas);        cas++;        printf("%d\n\n",dp[(1<<n)-1]);    }    return 0;}