POJ 3211 Washing Clothes(???)

来源:互联网 发布:江南七怪 知乎 编辑:程序博客网 时间:2024/06/08 19:35
Washing Clothes
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 10025 Accepted: 3225

Description

Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a beautiful and hard-working girlfriend to help him. The clothes are in varieties of colors but each piece of them can be seen as of only one color. In order to prevent the clothes from getting dyed in mixed colors, Dearboy and his girlfriend have to finish washing all clothes of one color before going on to those of another color.

From experience Dearboy knows how long each piece of clothes takes one person to wash. Each piece will be washed by either Dearboy or his girlfriend but not both of them. The couple can wash two pieces simultaneously. What is the shortest possible time they need to finish the job?

Input

The input contains several test cases. Each test case begins with a line of two positive integers M and N (M < 10, N < 100), which are the numbers of colors and of clothes. The next line contains M strings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then follow N lines describing the clothes. Each of these lines contains the time to wash some piece of the clothes (less than 1,000) and its color. Two zeroes follow the last test case.

Output

For each test case output on a separate line the time the couple needs for washing.

Sample Input

3 4red blue yellow2 red3 blue4 blue6 red0 0

Sample Output

10
这是个变化的0-1背包问题。俩个人洗衣服必须把同一种颜色的洗完,才可以洗下一种颜色的衣服。有多少种颜色的衣服就有多少个背包来处理吧!!!AC代码:#include<iostream>  #include<cstring>  #include<cstdio>  #include<algorithm>  using namespace std;  char ss[13][13],c[13];  int dp[100008],total[12];  int mun;    struct node{      int a,b;  }q[108];    bool cmp(node x,node y){      return x.b<y.b;  }  int main(){      int n,m,i,j,sum;      char c[13];      while(scanf("%d%d",&n,&m) && n && m){          mun=sum=0;          memset(total,0,sizeof(total));          for(i=1;i<=n;i++){              cin>>c;              strcpy(ss[mun++],c);  //把各种颜色放入数组里面          }          //printf("%d\n",mun);          for(i=1;i<=m;i++){              cin>>q[i].a>>c;              for(j=0;j<mun;j++){                  if(!strcmp(ss[j],c)){  //判断,并把颜色分组,并标记下标。。。。。                      q[i].b=j;break;                  }               }              total[q[i].b]+=q[i].a;  //统计同一种颜色衣服的件数。。。          }          //printf("%d %d %d\n",total[0],total[1],total[2]);          sort(q+1,q+1+m,cmp);            j=0;          for(i=0;i<mun;i++){  //求标记为i的颜色的时间              memset(dp,0,sizeof(dp));                  for( ; q[j].b==i && j<=m ; j++){                  //printf("  %d %d %d\n",j,q[i].b,i);                  for(int z=total[i]/2;z>=q[j].a;z--){                       dp[z]=max(dp[z],dp[z-q[j].a]+q[j].a);                  }              }              //printf("\n%d %d\n",dp[total[i]/2],j);              sum+=total[i]-dp[total[i]/2];            }          printf("%d\n",sum);      }      return 0;  }  

#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <vector>#include <map>#include <cmath>#include <numeric>using namespace std;vector<int> total[15];int dp[60*1000];int m,n;int main(){map<string,int>ma;int w;char s[60];while(~scanf("%d%d",&m,&n)&&(n+m)){ma.clear();for(int i=0;i<m;i++){scanf("%s",&s);ma[s]=i;}for(int i=0;i<n;i++){scanf("%d%s",&w,&s);total[ma[s]].push_back(w);}int sum=0,ans=0;for(int i=0;i<m;i++){ memset(dp,0,sizeof(dp));  sum=accumulate(total[i].begin(),total[i].end(),0); for(int j=0;j<total[i].size();j++) { for(int k=sum/2;k>=total[i][j];k--) { dp[k]=max(dp[k],dp[k-total[i][j]]+total[i][j]); } } ans+=sum-dp[sum/2];}printf("%d\n",ans);for(int i=0;i<m;i++) total[i].clear();}return 0;}


                                             
0 0