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;
}

转载于:https://www.cnblogs.com/lzmfywz/articles/2367179.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值