POJ __2923 Relocation

本文介绍了一道 ACM 竞赛题目,涉及搬家问题,其中包含两辆卡车和多个物品,每辆车有特定载重限制。问题求解的是最少需要多少次搬运来完成所有物品的运输。文章提供了输入输出样例,并探讨了解题策略,可能包括状态压缩和动态规划的方法。
摘要由CSDN通过智能技术生成

题目描述;

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:

2
6 12 13
3 9 13 3 10 11
7 1 100
1 2 33 50 50 67 98

output:

Scenario #1:
2

Scenario #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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值