POJ-3211 Washing Clothes

原题链接

Washing Clothes
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 9463 Accepted: 3032

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
先记录每种颜色所代表的衣服洗干净所用的时间,和与时间总和tot[i].对于第i颜色,用01背包找到0到tot[i]/2,洗衣服时可能遇到所有时间点,在找到0到tot[i]/2中最大的时间点j,则tot[i]-j则是洗第i中颜色的衣服所用的最少时间。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 1005
using namespace std;
typedef long long ll;

char s[15][15], k[15];
int p[15][105], num[15], tot[15];
int dp[50005];
int main(){
	
//	freopen("in.txt", "r", stdin);
	int n, m, a;
	
	while(scanf("%d%d", &n, &m) == 2){
		
		if(n == 0 && m == 0)
		 break;
		memset(num, 0, sizeof(num));
		memset(tot, 0, sizeof(tot));
		for(int i = 0; i < n; i++)
		  scanf("%s", s[i]);
		for(int i = 0; i < m; i++){
			scanf("%d%s", &a, k);
			for(int j = 0; j < n; j++){
				if(strcmp(s[j], k) == 0){
					p[j][num[j]] = a;
					tot[j] += a;
					num[j]++;
					break;
				}
			}
		}
		int cnt = 0;
		for(int i = 0; i < n; i++){
			memset(dp, 0, sizeof(dp));
			dp[0] = 1;
			for(int j = 0; j < num[i]; j++){
				for(int h = tot[i]/2; h >= p[i][j]; h--)
				  dp[h] |= dp[h-p[i][j]];
			}
			int h;
			for(h = tot[i]/2; h >= 0; h--){
				if(dp[h])
				 break;
			}
		    h = tot[i] - h;
		    cnt += h;
		}
		printf("%d\n", cnt);
	}
	return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值