1. 程式人生 > >poj 3211 Washing Clothes【0-1揹包】

poj 3211 Washing Clothes【0-1揹包】

 Washing Clothes
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 9213   Accepted: 2946

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 integersM

and N (M < 10, N < 100), which are the numbers of colors and of clothes. The next line containsM strings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then followN 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


題目大意:一個男的和他女盆友一起洗衣服,衣服的顏色最多分為十類,為了讓這些顏色不同的衣服不相互染色,得把顏色不同的衣服分開洗,給出每一件衣服的顏色以及所花的時間,求他們最少要花多少時間才能把所有衣服都洗完?


因為兩個人同一時間內只能洗相同顏色的衣服,所以可以把這些衣服分類,分別求出洗完各類顏色衣服所花的最少時間,求和即為結果,

對於每一種顏色的衣服,都可以用0-1揹包求其所花的最少時間,【上文中所說的最短時間,最理想的狀態為兩人洗的衣服所花的時間相等,無論如何都不會想等,就儘量讓兩人的時間差最小】。【用0-1揹包,可以把洗衣服所花的時間視為價值和體積】。

struct Clo{
    int color;		//衣服顏色;
    int num;		//該顏色的衣服總共有多少件;
    int sum;		//一個人要完這種顏色的衣服所花的時間;
    int time[101];	//洗每件衣服所花的時間;
}clo[12];		//因為衣服的顏色最多隻有10種。

已Accept程式碼【c++提交】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m;
int dp[100005];
char str[11][12];
struct Clo {
	int time[101];
	int num;
	int color;
	int sum;
}clo[11];

void solve() {
	int total = 0;
	for(int i = 0; i < n; i++) {
		memset(dp, 0, sizeof(dp));
		int sum = clo[i].sum / 2;
		for(int j = 0; j < clo[i].num; j++) {
			for(int k = sum; k >= clo[i].time[j]; k--) {
				dp[k] = max(dp[k], dp[k - clo[i].time[j]] + clo[i].time[j]);
			}
		}
		total += clo[i].sum - dp[sum];
	}
	printf("%d\n", total);
}

int main() {
	char buf[15];
	int time;
	while(scanf("%d%d", &n, &m), n | m) {
		for(int i = 0; i < n; i++) {
			scanf("%s", str[i]);
			clo[i].num = clo[i].sum = 0;
			clo[i].color = i;
		}
		for(int i = 0; i < m; i++) {
			scanf("%d %s", &time, buf);
			for(int j = 0; j < n; j++)
				if(!strcmp(str[j], buf)) {
					int tmp = clo[j].num;
					clo[j].time[tmp] = time;
					clo[j].sum += time;
					clo[j].num++;
					break;
				}
		}
		solve();
	}
	return 0;
}