poj 3211 Washing Clothes 分组背包?

来源:互联网 发布:河南网络知识竞赛 编辑:程序博客网 时间:2024/05/17 02:59

Washing Clothes
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 9241 Accepted: 2956

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

Source

POJ Monthly--2007.04.01, dearboy




总时间=每个洗每个颜色所需(最小)时间之和

对于每种颜色

bool dp[x],

代表花费x时间能否达到。

假设某种颜色最多花V时间完成。

只需看从v=V/2 逐渐减小到0  ,能否满足dp[v]==true&&dp[V-v]==true,若满足,所需时间t=max(dp[v],dp[V-v]);

求这个最小t。


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn= 100+10   ;const int maxm= 12   ;int n,m;map<string ,int >mp;string s;struct Thing{    int cost;    Thing(){}    Thing(int cost):cost(cost){}} ;vector<Thing>  G[maxm];bool dp[1000*maxn+20];int V[maxm];int main(){  while(~scanf("%d%d",&m,&n)&&(m||n))  {      mp.clear();      for(int i=1;i<=m;i++) { cin>>s;mp[s]=i;}      for(int i=1;i<=m;i++) G[i].clear();      memset(V,0,sizeof V);      for(int i=1;i<=n;i++)      {          int cost;          scanf("%d",&cost);          cin>>s;          int type=mp[s];          G[type].push_back(Thing(cost));          V[type]+=cost;      }      int ans=0;      for(int type=1;type<=m;type++)      {          memset(dp,0,sizeof dp[0]*(V[type]+1) );          dp[0]=1;          for(int i=0;i<G[type].size();i++)          {            Thing & now=G[type][i];              int cost =now.cost;              for(int v=V[type];v>=cost;v--)              {                  dp[v]=dp[v]|dp[v-cost];              }          }          int mini=INF;          for(int st=V[type]/2;st>=0;st--)          {              if(dp[st]&& dp[V[type]- st] )  mini=min(mini,max(st,V[type]-st   )         );          }          ans+=mini;      }      printf("%d\n",ans);  }    return 0;}


0 0
原创粉丝点击