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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值