Relocation

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:


At their old place, they will put furniture on both cars.
Then, they will drive to their new place with the two cars and carry the furniture upstairs.
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 n, C1 and C2. C1 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最大为10,所以可以想象成长度为n的01字符串,将其进行
状态压缩,即可以用0~2^n-1这些数字来表示,每个数字代表
一种状态,紧接着利用“&”判断两种状态是否有交集和“|”求
两种状态的并集,可以依次算出载重量c1、c2的汽车可以运载的状态
再算出两辆车一起时可以运载的状态,如此,为dp(01背包)做好了
准备,只不过是以状态来表示货物

# include<stdio.h>
# include<string.h>
int main()
{
    int num,n,i,j,c1,c2,c,a1,a2,l,k;
    int w[11],t1[1<<10],t2[1<<10],t[1<<10],tt[1<<10],dp[1<<10];
    scanf("%d",&num);
    for (k=1;k<=num;k++)
    {
        scanf("%d%d%d",&n,&c1,&c2);
        for (i=0;i<n;i++) scanf("%d",&w[i]);
        a1=a2=0;
        for (i=0;i<(1<<n);i++)
        {
            c=0;
            for (j=0;j<n;j++)
            {
                if ((i&(1<<j))!=0) c+=w[j];
            }
            if (c<=c1) t1[a1++]=i;
            if (c<=c2) t2[a2++]=i;
        }       //求c1、c2的汽车分别能装哪些状态
        memset(t,0,sizeof(t));
        for (i=0;i<a1;i++)
        for (j=0;j<a2;j++)
        {
            if ((t1[i]&t2[j])==0)
            t[t1[i]|t2[j]]=1;
        }       //将两辆车一起时可运状态赋为1,即进行标记
        l=0;
        for (i=0;i<(1<<n);i++)
        if (t[i]==1)
        {
            tt[l++]=i;
        }       //求出哪些状态是可以利用两辆车一起来运载的
        memset(dp,-1,sizeof(dp));
        dp[0]=0;
        for (i=0;i<(1<<n);i++)
        if (dp[i]>=0)
        for (j=0;j<l;j++)
        if ((tt[j]&i)==0)
        {
            int sum;
            sum=(tt[j]|i);
            if (dp[sum]==-1) dp[sum]=dp[i]+1;
            else dp[sum]=dp[sum]<(dp[i]+1)?dp[sum]:dp[i]+1;
        }    //背包模型求dp[i],将状态i运走需要的最少次数
        printf("Scenario #%d:\n",k);
        printf("%d\n",dp[(1<<n)-1]);
        printf("\n");
    }
}


最后就是普通的01背包模型,dp[i]表示i状态下的最少运算次数
最后注意一下的是,这道题每个数据的输出后要加一空行,细节也是很
重要的哦,白白的PE了一次。。。
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值