POJ 2923 relocation (shaped pressure dp+01 backpack)

Source: Internet
Author: User
Tags cas min printf
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:

At their old place, they would put furniture on both cars.
Then, they'll drive to their new place with the both cars and carry the furniture upstairs.
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 both cars, how 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 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 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

2 6, 3 9, 3, 7 1,
1 2 33 50 50 67 98
Sample Output
Scenario #1:
2

Scenario #2:
3
Main Topic

There are n pieces of furniture with a weight of W, and there are two vehicles bearing C1,C2, asking how many times it will take to carry the furniture. Thinking of solving problems

Bundle n pieces of furniture, use 1,0 to indicate whether the current state chooses this furniture, carries on the state compression;
Then the total number of binding methods that is the total number of States is (1<<n) (1, with mark[] mark which can be used c1,c2 two vehicles one-off bundled state;
In the mark[] in the selection of independent (does not contain the same furniture) bundle status of 01 backpack, each bundle state I is equivalent to an item, its volume is mark[i], value equivalent to 1 (the contribution to the last transport number is 1), Dp[i] represents the minimum number of times required to transport the state I, So the minimum number of times that all furniture will be transported is dp[(1<<n) −1] dp[(1.
Equation of State transition: dp[j|mark[i]]=min (dp[j|mark[i]],dp[j]+1) dp[j|mark[i]]=min (dp[j|mark[i]],dp[j]+1), J and mark[] two states satisfy each other independently. Code Implementation

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
#define MAXN 1107 #define MAXM 107 const int INF=0X3F3F3F3F;
int DP[MAXN],MARK[MAXN];
int W[MAXM];
int n,c1,c2;
BOOL VIS[MAXN];
    BOOL judge (int x) {int sum=0;  memset (vis,0,sizeof (VIS));
    Mark I can use C1 one time to transport vis[0]=1;  for (int i=0;i<n;i++)//Enumerate each furniture {if (x& (1<<i))//This furniture is bundled in the current state {sum+=w[i];
                    Marks the total weight of the furniture contained in the current state for (int j=c1-w[i];j>=0;j--) if (Vis[j])//If J can be shipped with C1 once, then J+w[i] can also
        Vis[j+w[i]]=1;  }} for (int i=c1;i>=0;i--) if (VIS[I]&AMP;&AMP;SUM-I&LT;=C2) return true;
The current state can be transported with two vehicles at once return false;
    } int main () {int T;
    scanf ("%d", &t);
        for (int cas=1;cas<=t;cas++) {memset (dp,inf,sizeof (DP));
        scanf ("%d%d%d", &AMP;N,&AMP;C1,&AMP;C2);
        if (C1&LT;C2) swap (C1,C2); for (int i=0;i<n;i++) scanf ("%d",&W[i]);
        int t=0;   for (int i=1;i< (1<<n); i++)//traverse the status of each bundle if (judge (i))//Determine if the current state can be transported at once Mark[++t]=i;
        Mark Dp[0]=0 for a state that can be transported at a time;
            for (int i=1;i<=t;i++)//enumeration can be shipped one time for the state {for (int j= (1<<n) -1;j>=0;j--)//Enumerate all the states of the bundle
                {if (dp[j]==inf) continue;
            if ((J&mark[i]) ==0)//two states are independent of each other, that is, does not contain the same furniture dp[j|mark[i]]=min (dp[j|mark[i]],dp[j]+1);
        }} printf ("Scenario #%d:\n", CAs);
    printf ("%d\n\n", dp[(1<<n)-1]);
} return 0;
 }

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.