I - Relocation
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit StatusDescription
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 98Sample Output
Scenario #1:
2
Scenario #2:
/*状态压缩加DP就是把一次能运走的,当成一个物品总状态量为1<2、“按位或”运算符(|) 两个相应的二进制位中只要有一个为1,该位的结果值为1。借用逻辑学中或运算的话来说就是,一真为真左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指定(右操作数必须是非负右移运算符(>>) 右移运算符是用来将一个数的各二进制位右移若干位,移动的位数由右操作数指定(右操作数必须是非负 值),移到右端的低位被舍弃,对于无符号数,高位补0。对于有符号数,某些机器将对左边空出的部分 用符号位填补(即“算术移位”)*//*状态压缩加DP就是把一次能运走的,当成一个物品总状态量为1<>) 右移运算符是用来将一个数的各二进制位右移若干位,移动的位数由右操作数指定(右操作数必须是非负 值),移到右端的低位被舍弃,对于无符号数,高位补0。对于有符号数,某些机器将对左边空出的部分 用符号位填补(即“算术移位”)*/
PRE class=html name="code">//状态压缩加DP #include#include #include #define inf 0x4f4f4f4f using namespace std; int n,v1,v2; int state[1<<11]; bool visit[200]; int cost[1<<11],dp[1<<11]; int minx(int a,int b)//返回最小值 { if(av1+v2)//如果一次要运的总值,比两辆车的总容量还大,当然要去掉 { return false; } for(j=v1;j>=cost[i];j--) { if(visit[j-cost[i]])//visit[v1-cost[i]]为真说明,在前面的值会取到,这时再加一个cost [j],那么visit[j]也能取到,所以就是为真 { visit[j]=true; } } } } for(i=0;i<=v1;i++) { if(visit[i]&&sum-i<=v2)//如果第一辆车装了I的容量,剩下的sum-i比第二辆小,当然,可以动输返连回真! return true; } return false; } int main() { int t,total,tcase,i,v; scanf("%d",&t); for(tcase=1;tcase<=t;tcase++) { printf("Scenario #%d:\n",tcase); scanf("%d%d%d",&n,&v1,&v2); for(i=0;i =0;v--) { if((state[i]&v)==0)//新加入的物品,没有被以前的已经运走! { if(dp[v]==inf) continue; dp[v|state[i]]=minx(dp[v]+1,dp[v|state[i]]);//合在一起的状态 } } printf("%d\n\n",dp[(1<