Poj 3211 portable Clothes 0-1 backpack

Source: Internet
Author: User

Poj 3211 portable Clothes 0-1 backpack

Question:

Two people wash n pieces of clothing, each of which has the time and color to wash. They can only wash the clothes of the same color.

Analysis:

The 0-1 backpack determines whether a certain weight can be reached.

Code:

//poj 3211//sep9#include 
 
  #include 
  #include 
   
    using namespace std;const int maxN=128;int m,n;map
    
      name; int v[maxN][maxN];int dp[100028];int pack(int k){memset(dp,0,sizeof(dp));dp[0]=1;int i,j,tot=0;for(i=1;i<=v[k][0];++i)tot+=v[k][i];int ans=tot;for(i=1;i<=v[k][0];++i){int w=v[k][i];for(j=tot;j>=w;--j){dp[j]=max(dp[j],dp[j-w]);if(dp[j]==1)ans=min(ans,max(j,tot-j));}}return ans;}int main(){while(scanf("%d%d",&m,&n)==2){if(m==0&&n==0)break;while(m--)scanf("%*s");int num=0;name.clear();for(int i=0;i
     
      >x>>a;if(name[a]==0){name[a]=++num;v[name[a]][0]=0;}v[name[a]][++v[name[a]][0]]=x;}int ans=0;for(int i=1;i<=num;++i)ans+=pack(i);printf("%d\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.