poj 3211 Washing Clothes(分组01背包)

夫妻俩洗衣服,他俩一块洗,必须要一块先洗完同一种颜色的衣服,才能洗另一种颜色的衣服。
按照衣服颜色分类,对于每种颜色的衣服进行01背包,最后加和。
对于每种颜色,背包容量就是洗完当前颜色所有衣服花费总时间的一半。因为要求时间最短,所以夫妻俩洗衣服的时间相差越小越好,所以是一半喽,然后算出这一半时间内的洗衣服最多花费多长时间,总时间减去他,就是他对象洗衣服花费的时间,就是那个较长的时间。

#include <stdio.h>
#include <string.h>
#define max(a,b) a>b?a:b
#define min(a,b) a>b?b:a
struct Clothes
{
    char color[12];
    int cnt;
    int sumTime;
    int time[110];
};
Clothes cs[15];
const int MAXN = 100010;
int dp[MAXN];
int M,N,tm;
char str[12];
int res;

void solve()
{
    res = 0;
    int W;
    for(int c = 0; c < M; ++c)
    {
        memset(dp,0,sizeof(dp));
        W = cs[c].sumTime/2;
        for(int i = 0; i < cs[c].cnt; ++i)
        {
            for(int j = W; j >= cs[c].time[i]; --j)
            {
                dp[j] = max(dp[j],dp[j-cs[c].time[i]]+cs[c].time[i]);
            }
        }
        res += cs[c].sumTime-dp[W];
    }
}

int main()
{
    while(scanf("%d %d",&M,&N) && (N+M))
    {
        memset(cs,0,sizeof(cs));
        for(int i = 0; i < M; ++i)
            scanf("%s",cs[i].color);
        for(int i = 0; i < N; ++i)
        {
            scanf("%d %s",&tm,str);
            for(int k = 0; k < M; ++k)
            {
                if(!strcmp(cs[k].color,str))
                {
                    cs[k].sumTime += tm;
                    cs[k].time[cs[k].cnt] = tm;
                    cs[k].cnt++;
                }
            }
        }
        solve();
        printf("%d\n",res);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值