poj2923--Backpack DP (01+-like pressure)--relocation

Source: Internet
Author: User

Description

Emma and Eric is moving to their new house they bought after returning from their honeymoon. Fortunately, they has a few friends helping them relocate. To move the furniture, they only has the 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 would have the to does several trips to transport Everyth Ing. The schedule for the move was planed like this:

    1. At their old place, they would put furniture on both cars.
    2. Then, they'll drive to their new place with the both cars and carry the furniture upstairs.
    3. Finally, everybody would return to their old place and the process continues until everything was moved to the new place.

Note, that the group was always staying together so, they can has 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 What many trips to the new House does the party has to do 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 is 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 would contain n integers w1, ..., wn, the weights of the furniture (1≤ WI ≤100). It is guaranteed, piece of furniture can be loaded by at least one of the both cars.

Output

The output for every scenario begins with a line containing "scenario #i:", where i am The number of the scenario starting at 1. Then print a single line with the number of trips to the new house they has to do to move all the furniture. Terminate each scenario with a blank line.

Sample Input

26 12 133 9 13 3 10 117 1 1001 2 33 50 50 67 98

Sample Output

Scenario #1:2Scenario #2:3
The main idea: give you two cars, n items, the total number of items not more than 10 pieces, each car has a load limit, from the question of how many times to transport
First of all, the bit operation: & To see if there is the same, equal to and, | is combined, equal to or.
Read the online code, the situation as a 01 backpack items, altogether (1<<n)-1 items (note: bit operation priority is lower than minus), each item consumes space is a[i], if not taken, then dp[j]+1, take Dp[j|a[i]] as a binary, is to take J and A[i] all the situation out (of course!) (J&a[i]) that is not the same, then now is to ask each situation of the consumption of space, we see C1 as the total space, X is the current acquisition of the situation (such as 010101111) 1 to take the things, 0, to represent what is not taken,x& (1<< i) the meaning is to take I, the weight has been taken out, if it has been taken and (x this case) the rest can let C2, then it is feasible, similar to greedy, optimization, then the 01 backpack, look at the minimum number of times required to consume.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAX = -;Const intINF =0x3f3f3f3f;intW[max],dp[max],vis[max],a[max];intn,c1,c2;BOOLCheckintx) {    intsum =0; memset (Vis,0,sizeof(VIS)); vis[0] =1;  for(inti =0; I < n; i++){        if(x& (1<<i)) {Sum+=W[i];  for(intj = C1-w[i]; J >=0; j--)                if(Vis[j]) vis[w[i]+J] =1; }    }    for(inti = C1; I >=0; i--)        if(Vis[i] && sum-i <=C2)return 1; return 0 ;}intMain () {intT; scanf ("%d",&T);  for(inttemp =1; Temp <= T; temp++) {scanf ("%d%d%d",&n,&c1,&C2); intMax1 = (1<< N)-1; Memset (A,0,sizeof(a)); Memset (DP,0,sizeof(DP));  for(inti =1; i<=max1; i++) Dp[i]=inf; dp[0] =0;  for(inti =0; I < n; i++) scanf ("%d",&W[i]); intres =1;  for(inti =1; I <= max1; i++)          if(check (i)) A[res++] =i;  for(inti =1; I <= res; i++)             for(intj = 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:\n%d\n\n", Temp,dp[max1]); }    return 0;}
View Code



poj2923--Backpack DP (01+-like pressure)--relocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.