首页 > 其他 > 详细

Relocation 状态压缩DP

时间:2014-07-14 10:57:36      阅读:382      评论:0      收藏:0      [点我收藏+]
 Relocation
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <map>
 5 using namespace std;
 6 int c1,c2,vi[110],a[30],n,dp[1100];
 7 bool check(int x)
 8 {
 9     int i,j,sum=0;
10     memset(vi,0,sizeof(vi));
11     vi[0]=1;
12     for(i=0;i<n;i++)
13     {
14         if(x&(1<<i))
15         {
16             sum+=a[i];
17             for(j=109;j>=a[i];j--)
18             if(vi[j-a[i]])
19             vi[j]=1;
20         }
21     }
22     for(i=c1;i>=0;i--)
23     {
24         if(vi[i]&&sum-i<=c2)return 1;
25     }
26     return 0;
27 }
28 int main()
29 {
30     int t,i,j,b[(1<<11)],bn,sum,cas=1;
31     scanf("%d",&t);
32     while(t--)
33     {
34         sum=0;
35         scanf("%d%d%d",&n,&c1,&c2);
36         if(c1>c2)swap(c1,c2);
37         for(i=0;i<n;i++)
38         scanf("%d",&a[i]),sum+=a[i];
39         bn=0;
40         for(i=1;i<(1<<n);i++)
41         {
42             if(check(i))
43             b[bn++]=i;
44         }
45         for(i=0;i<1<<n;i++)dp[i]=20;
46         dp[0]=0;
47         for(i=0;i<bn;i++)
48         {
49             for(j=(1<<n)-1;j>=0;j--)
50             if((b[i]|j)==j)
51             dp[j]=min(dp[j],dp[b[i]^j]+1);
52         }
53         cout<<"Scenario #"<<cas++<<":"<<endl;
54         cout<<dp[(1<<n)-1]<<endl<<endl;
55     }
56 }
View Code

 

Relocation 状态压缩DP,布布扣,bubuko.com

Relocation 状态压缩DP

原文:http://www.cnblogs.com/ERKE/p/3841959.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!