poj2923 Relocation
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就是把一次能运走的,当成一个物品<PRE><SPAN style="COLOR: rgb(0,128,0)">总状态量为1<<n,判断这些状态集合里的那些物品能否一次就 运走,如果能运走,那就把这个状态看成一个物品。预处理完能 从枚举中找到tot个物品,再用这tol个物品中没有交集 (也就是两个状态不能同时含有一个物品)的物品进 行01背包,每个物品的体积是state[i],can函数,就是判断能否一次把给定的物品运走!注意位运算的顺序!</SPAN></PRE><PRE><SPAN style="COLOR: rgb(0,128,0)">2、“按位或”运算符(|) 两个相应的二进制位中只要有一个为1,该位的结果值为1。借用逻辑学中或运算的话来说就是,一真为真</SPAN></PRE><PRE><SPAN style="COLOR: rgb(0,128,0)">左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指定(右操作数必须是非负</SPAN></PRE><PRE><SPAN style="COLOR: rgb(0,128,0)">右移运算符(>>) 右移运算符是用来将一个数的各二进制位右移若干位,移动的位数由右操作数指定(右操作数必须是非负 值),移到右端的低位被舍弃,对于无符号数,高位补0。对于有符号数,某些机器将对左边空出的部分 用符号位填补(即“算术移位”)*/</SPAN></PRE> /*状态压缩加DP就是把一次能运走的,当成一个物品总状态量为1<<n,判断这些状态集合里的那些物品能否一次就 运走,如果能运走,那就把这个状态看成一个物品。预处理完能 从枚举中找到tot个物品,再用这tol个物品中没有交集 (也就是两个状态不能同时含有一个物品)的物品进 行01背包,每个物品的体积是state[i],can函数,就是判断能否一次把给定的物品运走!注意位运算的顺序!2、“按位或”运算符(|) 两个相应的二进制位中只要有一个为1,该位的结果值为1。借用逻辑学中或运算的话来说就是,一真为真左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指定(右操作数必须是非负右移运算符(>>) 右移运算符是用来将一个数的各二进制位右移若干位,移动的位数由右操作数指定(右操作数必须是非负 值),移到右端的低位被舍弃,对于无符号数,高位补0。对于有符号数,某些机器将对左边空出的部分 用符号位填补(即“算术移位”)*/
PRE class=html name="code">//状态压缩加DP #include <iostream> #include<stdio.h> #include<cstring> #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(a<b) return a; else return b; } bool can(int x) { int i,j,sum; memset(visit,false,sizeof(visit)); sum=0; visit[0]=true;//一定要把0初始化为真 for(i=0;i<n;i++) { if((1<<i)&x)//取X的第I位,判断是否运走 { sum+=cost[i];//要运走的总值加起来 if(sum>v1+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<n;i++) { scanf("%d",&cost[i]); } for(i=0,total=0;i<(1<<n);i++) { if(can(i))//判断一次能不能把这几个物品运走,能运走返回TRUE { state[total]=i; //总的状态数,一个状态,相当于一个物品,也就转化成了背包问题 total++; } } for(i=0;i<(1<<n);i++) { dp[i]=inf;//保证所有的物品都要取到 } dp[0]=0;//到此,已经成了DP问题,就是一个01背包问题了! for(i=0;i<total;i++) for(v=(1<<n)-1;v>=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<<n)-1]); } return 0; } </PRE><BR><BR>
补充:软件开发 , C++ ,