RELOCATION-POJ 2923 (state compression +01 backpack)

Source: Internet
Author: User

The main topic: there is a person need to move, there are n items, to the weight of an item is w[i] and then two cars, each car's load capacity is C1 and C2, the minimum need to transport how many times to put these items all transport finished.

Analysis: Just start to find the number of items, think of directly first enumerate a car transport items, and then calculate how many times it will transport these items, but later found that the complexity is a bit high, another good solution is to enumerate two cars once can be transported goods, and then to use these can be transported to each other to match the transfer of the State, Dp[i] Indicates the minimum number of transport times to reach the state I, and it is also a simple 01 knapsack problem to find out if a certain state can be transported once.

The code is as follows:

=============================================================================================================== ========

#include <stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;Const intMAXN =1<<Ten;Const intBit =Ten;Const intOO = 1e9+7;intN, Ca, Cb;BOOLJudge (intXintw[]) {///determine if the status is X can be transported once    intv[107]={1}, sum=0;  for(intI=0; i<n; i++)if(X & (1<<i)) {sum+=W[i];  for(intJ=ca-w[i]; j>=0; j--)        {            if(V[j]) v[j+w[i]] =true; }    }    if(Sum > CA+CB)return false;  for(intI=0; i<=ca; i++)    {        if(V[i] && Cb >= sum-i)return true; }    return false;}intMain () {intT, t=1; scanf ("%d", &T);  while(t--)    {        intI, J, M, W[MAXN], F[MAXN], k=0; scanf ("%d%d%d", &n, &ca, &b);  for(i=0; i<n; i++) scanf ("%d", &W[i]);  for(i=0, m=1<<N; i<m; i++)        {            if(Judge (i, W)) f[k++] =i; }        intdp[maxn]={0};  for(i=1; i<m; i++) Dp[i]=Oo;  for(i=0; i<k; i++)         for(j=0; j<m; J + +)        {            if((j&f[i]) = =0) {dp[j|f[i]] = min (Dp[j|f[i]], dp[j]+1); }} printf ("Scenario #%d:\n%d\n\n", t++, dp[m-1]); }    return 0;}

RELOCATION-POJ 2923 (state compression +01 backpack)

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.