Algorithm: POJ 2923 Relocation (enum + backpack | State compression +01 Backpack)

Source: Internet
Author: User
Tags bool cas continue printf

The main effect of the topic:

There are n pieces of furniture, each of which weighs (1≤wi≤100) and is transported to the destination with two handlebar vehicles. The two vehicles have a load limit of C1, C2 (1≤ci≤100), asking at least a few trips to get all the furniture to the destination.

The two cars must go with each other, even if a car is not carrying anything.

Ideas:

A

First on your own way:

Enumerating all the combinations of furniture that the first vehicle may carry, then a binary representation of the state is a total of 1<<n, and if the binary is 1, then the first piece of furniture is shipped.

This would be tantamount to dividing all the furniture into two groups, one for the first and the other for the second.

Then the two sets of data, respectively, can be transported at least several times, so that the problem is converted to: to n furniture, and a load of weight C of the car, the car at least several times can be carried out.

By the greedy thought can know that each delivery should be as full as possible to fill the car, then with 01 backpacks, backpack capacity to load the weight of C, the weight of each piece of furniture is the value is also cost, f[c] is this delivery. After the selection, the selected furniture removed, continue to do 01 backpack selection, until all the furniture selected. As for the records selected furniture, method and record path.

The two cars were then shipped in a larger number, the minimum number of times this was enumerated.

Two

This method is to learn, the basic online is this method, no longer fine talk.

The idea is to use the binary to express the choice of the state, the first pretreatment of all the selection of combinations, see whether the group selected furniture can be two cars once, if you can "pack" up as an object, and then to all the objects carry 01 backpacks.

Code:

Method One:

#include &lt;iostream&gt; #include &lt;queue&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; #include &LT;CMATH&G  
T #include &lt;map&gt; #include &lt;vector&gt; #include &lt;string&gt; #define MP make_pair #define SQ (x) ((x) * (x)) C  
Onst Double PI = ACOs (-1.0);  
    
const int INF = 0X3F3F3F3F;  
using namespace Std;  
    
    
typedef long long Int64;  
    
const int MAXN = 12;  
int n, m;  
int C1, C2;  
int f[110];  
int W[MAXN];  
int ans;  
BOOL path[maxn][110];  
    
    
VECTOR&LT;INT&GT;W1, W2; BOOL Check (int x) {for (int i=0; i&lt;n; ++i) {if ((x&gt;&gt;i) &amp;1) {if (W[I]&GT;C1) return  
        False  
        }else{if (W[I]&GT;C2) return false;  
} return true;  
    } void init (int x) {w1.clear (); W2.clear ();  
        for (int i=0; i&lt;n; ++i) {if ((x&gt;&gt;i) &amp;1) {w1.push_back (w[i));  
    }else W2.push_back (W[i]); } void Update (VEctor&lt;int&gt;&amp;w, int i, int j) {if (i&lt;0) return;  
    int tmp = W[i];  
        if (Path[i][j]) {w.erase (W.begin () +i);  
    Update (w, i-1, j-tmp);  
}else Update (W, I-1, J);  
       
    int counter (vector&lt;int&gt;&amp; w, int c) {if (W.size () ==0) return 0;  
    int cnt = 0;  
        do{cnt++;  
        if (CNT &gt;= ans) return CNT;  
        memset (f, 0, sizeof (f));  
            
        memset (path, 0, sizeof (path)); for (int i=0; i&lt;w.size (); ++i) {for (int v=c; v&gt;=w[i];--v) {if (f[v-w[i]]+w[i) &gt;= F [v])  
                    {F[v] = F[v-w[i]]+w[i];  
                PATH[I][V] = 1;  
    }} update (W, w.size ()-1, c);  
    
    }while (W.size ());  
return CNT;  
    int main () {int ncase, Cas=1, X;  
    scanf ("%d", &amp;ncase);  
        while (ncase--) {printf ("Scenario #%d:\n", cas++); scanf"%d%d%d", &AMP;N,&AMP;C1,&AMP;C2);  
    
        for (int i=0; i&lt;n; ++i) scanf ("%d", &amp;w[i]);  
        ans = INF;  
    
        int sta = 0;  
                while (STA &lt; (1&lt;&lt;n)) {if (!check (STA)) {sta++;   
            Continue  
            Init (STA);  
            int cnt1 = Counter (W1, C1);  
            int cnt2 = counter (w2, C2);  
            ans = min (ans, max (cnt1, Cnt2));  
        sta++;  
    printf ("%d\n\n", ans);  
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.