POJ 3211 Washing Clothes

来源:互联网 发布:数值仿真软件 编辑:程序博客网 时间:2024/06/12 16:18

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 4
red blue yellow
2 red
3 blue
4 blue
6 red
0 0

Sample Output

10

思路

大意是这个男孩和他女盆友一起洗衣服,不过不同颜色的衣服不能一起洗,
也就是说他和他女盆友只可以一起洗同种颜色的衣服,给你颜色数,和多少衣服,问最少可有用多长时间洗完。

第一眼,这个题太麻烦,第二眼,怎么像01背包?第三眼,还真是用01背包做

统计衣服的颜色种数,每种颜色跑01背包,求和。

**#include<cstdio>#include<iostream>#include<cstdlib>#include<cstring> #define M 500010using namespace std;struct node{    int sum;//洗这种颜色衣服所用总时间    int num;//这种颜色衣服的件数     char color[20];//衣服的颜色     int ti[110];//每件衣服洗用的时间 } t[11];int f[M];int n,m;int hh(){    system("color 3e");    int time;    char col[20];    while(~scanf("%d%d",&m,&n)&&(n+m)) {        for(int i=1;i<=10;i++) {            t[i].num=0;            t[i].sum=0;        }        for(int i=1;i<=m;i++)           scanf("%s",t[i].color);        for(int i=1;i<=n;i++) {            scanf("%d%s",&time,col);            for(int j=1;j<=m;j++) {                if(!strcmp(col,t[j].color)){ //WA了千百遍,只因这的j写成了i (T_T)                    t[j].num++;                    t[j].ti[t[j].num]=time;                    t[j].sum+=time;                }               }        }        int ans=0;        for(int i=1;i<=m;i++) {            memset(f,0,sizeof(f));            int sum1=t[i].sum/2;            for(int j=1;j<=t[i].num;j++)              for(int k=sum1;k>=t[i].ti[j];k--)                  f[k]=max(f[k],f[k-t[i].ti[j]]+t[i].ti[j]);            ans+=t[i].sum-f[sum1];        }        printf("%d\n",ans);    }    return 0;}int hhh=hh();int main(){;}**
1 0