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 mostC.
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件物品,物品总数不超过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: %d ",temp,dp[max1]); } return 0; }