POJ 3211

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背包:因为要洗完一种颜色然后再洗另一种颜色,所以洗完这些衣服的最小时间就是洗完每一种颜色的衣服的最小时间之和, 而洗完每一种衣服的最小时间应该是贴近只有一个人洗完这种衣服时间的一半(因为现在有两个人洗),总时间减去两个人中用时最少的时间就是洗完这种衣服所用的最少时间了(也就是两人中用时较长的时间)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100050
#define INF 0x3f3f3f3f
int dp[maxn];
struct clothes{
    char color[12];
    int time[105];
    int time_sum;
    int num;
};
int main()
{
    int m, n, i, j, k, ans, piece, half;
    char c[12];
    while(~scanf("%d %d",&m, &n) && (m!=0||n!=0)){
        clothes clothe[15];
        for(i = 0; i < m; ++i){
            scanf("%s",&clothe[i].color);
            clothe[i].num = 0;
            clothe[i].time_sum = 0;
        }
        for(i = 0; i < n; ++i){
            scanf("%d %s",&piece,c);
            for(j = 0; j < m; ++j){
                if(!strcmp(c,clothe[j].color)){
                    clothe[j].time[clothe[j].num] = piece;
                    ++clothe[j].num;
                    clothe[j].time_sum += piece;
                }
            }
        }
        ans = 0;
        for(i = 0; i < m; ++i){
            half = clothe[i].time_sum/2;
            memset(dp,0,sizeof(dp));    
            for(k = 0; k < clothe[i].num; ++k){
                for(j = half; j >= clothe[i].time[k]; --j){
                    dp[j] = max(dp[j], dp[j-clothe[i].time[k]] + clothe[i].time[k]);
                }
            }   
            ans += (clothe[i].time_sum - dp[half]);
        }
        printf("%d\n",ans); 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值