POJ 3211 Washing Clothes

 

Time Limit: 1000MS
Memory Limit: 131072K

Total Submissions: 6708
Accepted: 1926

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
分析:

小两口洗衣服,只有一种颜色的衣服洗完才能洗下一种颜色,两人同时洗问需要的最小时间。

这里一种颜色的衣服所需的总时间可以求出,求出每种颜色衣服所需最小时间便求出了ans。每种颜色的衣服总时间有了,让求两个人最短完成时间,就相当于给你一个数让你分成两份,并让这两份尽可能的小,显然这个数的一半就是答案。在这里也就是两个人洗衣服的时间尽可能相同。

代码如下:

#include <iostream>
#include <cstring>
using namespace std;

char colors[105][15],color[15][15];
int dp[1000000],Time[105],Count,color_number,cloth_number,n,Time_sum[105],Color_num[15],Color_sum[15][105];

void DP()
{
    int i,j,k;
    int sum=0;
    int T;
    for(i=0;i<color_number;i++)
    {
        T=Time_sum[i]/2;
        for(j=0;j<=T;j++)
        {
            dp[j]=0;
        }
        for(j=0;j<=Color_num[i];j++)
        {
            for(k=T;k>=Color_sum[i][j];k--)
            {
                dp[k]=max(dp[k],dp[k-Color_sum[i][j]]+Color_sum[i][j]);
            }
        }
        sum+=(Time_sum[i]-dp[T]);
    }
    cout<<sum<<endl;
}

int main()
{
    int i,j;
    while(cin>>color_number>>cloth_number,color_number,cloth_number)
    {
        for(i=0;i<color_number;i++)
        {
            cin>>color[i];
        }
        for(i=0;i<cloth_number;i++)
        {
            cin>>Time[i]>>colors[i];
        }
        memset(Time_sum,0,sizeof(Time_sum));
        memset(Color_num,-1,sizeof(Color_num));
        for(i=0;i<color_number;i++)
        {
            for(j=0;j<cloth_number;j++)
            {
                if(!strcmp(color[i],colors[j]))
                {
                    Time_sum[i]+=Time[j];
                    Color_num[i]++;
                    Color_sum[i][Color_num[i]]=Time[j];
                }
            }
        }
        DP();
    }
    return 0;
}

posted on 2012-02-24 20:43  lzm风雨无阻  阅读(225)  评论(0编辑  收藏  举报

导航