码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3211 Washing Clothes

时间:2014-07-31 20:50:44      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   for   ar   

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

Source

额,01背包,开始以为贪心,我还是太年轻了不说了。策略如下:
统计每种颜色衣服需要的总时间sum,然后sum/2进行01背包,看能够装下的最大时间
具体见挫码=。=
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
int num[15][110];//统计每种颜色衣服各个数量所需时间
int nu[15];//统计每种颜色衣服的数量
char str[15][15];//读入字符
int dp[10000];//背包,开始开下了,wa了=。=
int n,m;
int main()
{
    int t;
    char s[110];
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        getchar();
        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            nu[i]=0;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%s",&t,s);
            for(int j=0;j<n;j++)
            {
                if(!strcmp(str[j],s))
                {
                    nu[j]++;
                    num[j][nu[j]-1]=t;
                }
            }
        }
        int ans=0;
        for(int i=0;i<n;i++)
        {
            if(nu[i])
            {
                int sum=0;
                for(int j=0;j<nu[i];j++)
                    sum+=num[i][j];
                memset(dp,0,sizeof(dp));//01背包枚举每种物品
                for(int j=0;j<nu[i];j++)
                {
                    for(int k=sum/2;k>=num[i][j];k--)
                        dp[k]=max(dp[k],dp[k-num[i][j]]+num[i][j]);
                }
                ans+=max(dp[sum/2],sum-dp[sum/2]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

POJ 3211 Washing Clothes,布布扣,bubuko.com

POJ 3211 Washing Clothes

标签:des   style   http   color   os   io   for   ar   

原文地址:http://blog.csdn.net/u013582254/article/details/38322223

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!