POJ2923——背包DP(01+状压)——Relocation

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

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件,每辆车都有负重上限,从问需要运多少次先说说位运算: &表示看是否有相同,相当于和,|表示合并起来,相当于或。看了网上的代码,把情况看作是01背包的物品,一共(1<<n)-1件物品(注意:位运算的优先级要低于减),每一件物品所消耗的空间是a[i],如果不取,那么dp[j]+1,取dp[j|a[i]]看做二进制,是把j和a[i]所有的情况都拿出来(当然前提是!(j&a[i])即没有相同的,那么现在就是要问每一种情况的消耗的空间,我们把c1看做是总的空间,x是当前取得情况(比如010101111)1代表拿的东西,0,代表没有拿的东西,x&(1<<i)意思就是取了i,把已经取了的重量标记一下,如果已经取了并且(x这种情况)剩下的能让c2运走的话,那么说明这种取得方式是可行的,类似贪心,最优化,然后就是01背包,看所需要消耗的最小的次数。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1200;
const int inf = 0x3f3f3f3f;
int w[MAX],dp[MAX],vis[MAX],a[MAX];
int n,c1,c2;
bool check(int x)
{
    int sum = 0;
    memset(vis,0,sizeof(vis));
    vis[0] = 1;
    for(int  i = 0; i < n ; i++){
        if(x&(1 << i)){
            sum += w[i];
            for(int j = c1 - w[i]; j >= 0 ; j--)
                if(vis[j])
                    vis[w[i]+j] = 1;
        }
    }
   for(int i = c1; i >= 0 ; i--)
        if(vis[i] && sum - i <= c2)
            return 1;
    return 0 ;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int temp = 1; temp <= T; temp++){
        scanf("%d%d%d",&n,&c1,&c2);
        int max1 = (1 << n) -1;
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        for(int i = 1; i<=max1; i++)
            dp[i] = inf;
        dp[0] = 0;
        for(int i = 0; i < n; i++)
            scanf("%d",&w[i]);
        int res = 1;
        for(int i = 1; i <= max1; i++)
          if(check(i))
              a[res++] = i;
        for(int i = 1; i <= res ;i++)
            for(int j = max1 - a[i]; j >=0 ;j --)
                if(!(j&a[i]))
                    dp[j|a[i]] = min(dp[j]+1,dp[j|a[i]]);
    printf("Scenario #%d:\n%d\n\n",temp,dp[max1]);
    }
    return 0;
}


				
时间: 2024-08-28 01:46:19

POJ2923——背包DP(01+状压)——Relocation的相关文章

dp乱写2:状态压缩dp(状压dp)炮兵阵地

https://www.luogu.org/problem/show?pid=2704 题意: 炮兵在地图上的摆放位子只能在平地('P') 炮兵可以攻击上下左右各两格的格子: 而高原('H')上炮兵能够攻击到但是不能摆放 求最多能摆放的炮兵的数量 就是这个意思. 难度提高,弱省省选 一开始是想写dfs(迷之八皇后)的, 但是看到数据量100就想dp了: 因为题目n的范围给的很少n<=10,想到状压 非常明显是一个状态压缩的dp(状压dp) 其实可以当做状压的入门题目来做. 由于本行的状态是由前若

HDU 4352 XHXJ&#39;s LIS (数位DP,状压)

题意: 前面3/4的英文都是废话.将一个正整数看成字符串,给定一个k,问区间[L,R]中严格的LIS=k的数有多少个? 思路: 实在没有想到字符0~9最多才10种,况且也符合O(nlogn)求LIS的特点,所以用状态压缩可以解决. 看到状态压缩的字眼基本就会做了,增加一维来保存当前LIS的状态.由于求LIS时的辅助数组d[i]表示长度为i的LIS最后一个元素,d数组是严格递增的,所以好好利用d数组的特性来设计状态压缩才是关键.压缩的状态0101可以表示:仅有0和2在数组d中,即d[1]=0,d[

[DP总结]状压DP

顾名思义,是用将状态进行二进制压缩成集合的形式来方便DP转移的方法. 一些常用的代码表示如下 i & j //取状态i,j重合部分 i ^ j //取状态i,j不同部分 i | j //合并状态i,j (1 << N) - 1 //表示111-1(N个1) 1 << i - 1 //表示00100-0(1后面有i-1个0,也就是有且仅有二进制下第i位为1) for (int i = 0; i < n; ++ i) if (x & (1 << i))

POJ 2923 【01背包+状态压缩/状压DP】

题目链接 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.

hoj 2662 经典状压dp // MyFirst 状压dp

题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2662 1.引言:用dp解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态. 但是有这样的一类题目,它们具有dp问题的特性,但状态中所包含的信息过多,如果要用数组来保存状态的话需要四维以上的数组. 于是,就要通过状态压缩来保存状态,而使用状态压缩来保存状态的dp就叫做状态压缩dp. 2.状态压缩dp的特点:状态中的某一维会比较小,一般不会超过15,多了的话状态数会急剧上升而无

SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)

题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? 思路: 这题比较好解决,只有前导零问题需要解决.如果枚举到011,那么其前导零(偶数)出现了1次而已,而此数11却是平衡数,所以不允许前导零的出现! 由于dfs时必定会枚举到前导零,否则位数较少的那些统计不到.状态需要3维or2维也行,3维的比较容易处理,用一维表示数位出现次数,另一维表示数位是否

codevs2596 售货员的难题(状压dp)

2596 售货员的难题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 钻石 Diamond 题目描述 Description 某乡有n个村庄(1<n<=15),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且A村到B村与B村到A村的路大多不同.为了提高效率,他从商店出发到每个村庄一次,然后返回商店所在的村,假设商店所在的村庄为1,他不知道选择什么样的路线才能使所走的路程最短.请你帮他选择一条最短的路. 输入描述 Input D

poj3254 Corn Fields (状压DP)

http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7588   Accepted: 4050 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parc

POJ2923--Relocation(01背包+状压dp)

果然对状压DP,我根本就不懂=.= /************************************************** Problem: 2923 User: G_lory Memory: 720K Time: 157MS Language: G++ Result: Accepted **************************************************/ #include <iostream> #include <cstring>