poj 2923 Relocation

Relocation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1529 Accepted: 618

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 most C.

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

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

Source

TUD Programming Contest 2006, Darmstadt, Germany

题目大意:

给你一些物品。每个物品有相应的重量。给你两辆载重不一定相同的车。问你最少要多少趟才能把所有物品运完。

思路:

状态压缩dp。二进制表示物品状态。0表示还没运走。1表示已经运走了。那么就可以枚举出两辆车一趟可一运出的状态。由于物品是一趟一趟运出来的。所以就可以由一个状态通过两辆车一趟的状态转移到另一个状态。

dp[i]=MIN(dp[k]+1)。k可以由两车一趟转移到i。

详细请看代码。

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define positive(a) ((a)>0?(a):-(a))
using namespace std;
int dp[1<<10];//dp[i]表示物品到i这个状态需要的最少趟数
int state[1<<10];//记录两辆车一趟可以运的状态
int n,cnt,w[15],can1,can2;//can1,can2两辆车的容量
void dfs(int num,int c1,int c2,int s)//用dfs搜出两辆车一趟可运出的状态
{                                    //num表示决策到第num个物品了。c1
                                     //表示第一辆车还剩的容量。c2表示第二
                                     //辆车还剩容量s表示决策后的状态
    if(num>=n)//如果物品全部决策完
    {
        if(!dp[s])//状态第一次出现记录状态。并标记
        {
            state[cnt++]=s;
            dp[s]=1;
        }
        return;
    }
    if(w[num]<=c1)//如果第一辆车能装
        dfs(num+1,c1-w[num],c2,s|(1<<num));
    if(w[num]<=c2)//如果第二辆车能装
        dfs(num+1,c1,c2-w[num],s|(1<<num));
    dfs(num+1,c1,c2,s);//两车都不装
}
int main()
{
    int t,cas,i,j,news;

    scanf("%d",&t);
    for(cas=1;cas<=t;cas++)
    {
        scanf("%d%d%d",&n,&can1,&can2);
        for(i=0;i<n;i++)
            scanf("%d",w+i);
        cnt=0;
        memset(dp,0,sizeof dp);
        dfs(0,can1,can2,0);
        memset(dp,0x3f,sizeof dp);
        dp[0]=0;
        for(i=0;i<(1<<n);i++)//枚举状态
        {
            for(j=0;j<cnt;j++)
            {
                if(i&state[j])//物品只能装一次。排除冲突
                    continue;
                news=i|state[j];//新状态
                dp[news]=MIN(dp[news],dp[i]+1);
            }
        }
        printf("Scenario #%d:\n%d\n\n",cas,dp[(1<<n)-1]);
    }
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值