POJ--2923--Relocation--状压DP

2015-01-24 05:45:03 · 作者: · 浏览: 3

Relocation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2288 Accepted: 950

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:

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个物品,两个车,车有自己的容量,物品有自己的体积,求最少的运送次数把物品全部运走,注意,两车一同发车。

解析:状态压缩DP,列举两车能够装下物品的所有方案,然后二进制找两车没有运相同物品的方案,然后用这些方案进行推算就出来了。

注意了,状态压缩就是用二进制的每一位来替代每个物品,所以n个物品的话最大就是n位


#include 
   
     #include 
    
      #include 
     
       #include 
      
        #define Max (1<<15) using namespace std; int main (void) { int t,n,c1,c2,i,j,k,l1,l2,L,cas=1; int s[11],dp[Max],s1[Max],s2[Max],dis[Max]; scanf("%d",&t); while(t--&&scanf("%d%d%d",&n,&c1,&c2)) { for(i=0;i
       
        -1) //预算的前提是这个基础点有值 { for(j=0;j
        
         dp[i]+1))//这里i&dis[j]==0是用来确定当前状态与当前方案没有冲突,冲突是指的当前状态已经用过某物品而这个方案正好要使用这个物品 dp[i|dis[j]]=dp[i]+1; } } printf("Scenario #%d:\n%d\n\n",cas++,dp[(1<
         
        
       
      
     
    
   

我觉得这个题很适用于状压DP的学习,我还刚接触,所以不会没有节操地大发厥词来写算法总结,嘎嘎嘎,先用两天再说。