Washing Clothes poj3211 0-1背包

Washing Clothes
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 8473 Accepted: 2635

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
 
题目大意:
   两个人洗衣服, 每种颜色的衣服有多件, 要求两人只能同时洗相同颜色的衣服, 求洗衣服的最短时间。

分析:因为只能同时洗相同颜色的衣服, 因此可将不同颜色的衣服看为不同的组, 分别求出来每组的最短时间, 其和即为所求;每组最短时间就是0 1背包;对于每种颜色洗它都有一个总时间,要求洗这种颜色的最少时间,就是求看能不能一个人洗这种颜色的衣服达到总时间的一半,也就是让两个人洗这种颜色的衣服的时间尽可能相同。

解题思路:先求出洗每种颜色的衣服所用的总时间,为了让两个人所用时间尽可能相同,把总时间的一半当做背包容量,每件衣服所花费的时间既是物体体积,又是物品价值,这样就转化为01背包,求背包的最大价值,然后用这种颜色的总时间减去最大价值就是洗这种颜色的衣服所用的最短时间。

#include<iostream>
#include<string.h>  
#include<algorithm>  
 using namespace std;
struct clothes
{
	int time[105]; //这种衣服中每一件用的时间
	int num;    //每种衣服的数量  
	int sum;  //洗每种衣服所用总时间 
	char color[12];   每种衣服颜色
}clo[12];
 int main()
 {
	 int m,n;
	 while(cin>>m>>n&&m+n)
	 {
		 int i,j,k;
		 for(i=0;i<m;++i)
		 {
			 cin>>clo[i].color;
			 clo[i].num=clo[i].sum=0;
		 }
		 char c[12];
		 int tim;
		 for(i=0;i<n;++i)
		 {
			 cin>>tim>>c;
			 for(j=0;j<m;++j)
			 {
				 if(!strcmp(c,clo[j].color))
				 {
					 int temp=clo[j].num;      //第j种第clo[j].num件,
					 clo[j].time[temp]=tim;
					 clo[j].num++;
					 clo[j].sum+=tim;
				 }
			 }
		 }
		 int ans=0,dp[100000];
		 for(i=0;i<m;++i)
		 {
			 memset(dp,0,sizeof(dp));
			 int tmp_sum=clo[i].sum/2;
			 for(j=0;j<clo[i].num;++j)
			 {
				 for(k=tmp_sum;k>=clo[i].time[j];--k)
				 {
					 dp[k]=max(dp[k],dp[k-clo[i].time[j]]+clo[i].time[j]);
				 }
			 }
			 ans += clo[i].sum - dp[tmp_sum]; 
		 }
		 cout<<ans<<endl;
	 }
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值