poj 3211 Washing Clothes

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

这道题属于分组背包的应用,但之前思路不对,一直不会做;看了dalao们的博客之后,发现此题应该是先用分组背包,之后再用0-1背包,总体思路便是求出每种颜色的衣服所需要的最短时间,最后加起来。中间细节的话再考虑每一件衣服,两个人所花费的时间越接近,当然最好,那么就用总时间的一半做0-1背包的最大容量(每件衣服的时间既是价值又是重量)。还是做题太少了,思路不够广阔,稍微难一点的就不会了,,,,真的是常敲代码手不抖啊啊啊啊~

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<string>
#include<algorithm>
#define N 1005
using namespace std;
int m,n,i,j,k,t,dp[N];
int halftime,sum;
string co;
struct Node{     //记录每一种颜色的结构体 
	int time[N]; //每种颜色之下的每种衣服的时间 
	int num;    //每种颜色之下的每种衣服的编号 
	int times;   //每种颜色之衣服的总时间 
	string color; 
}a[N];
int main()
{
	//freopen("3211.in","r",stdin);
	//freopen("3211.out","w",stdout);
	while(scanf("%d %d",&m,&n)!=EOF)
	{
		if(m==0&&n==0)
		break;
		for(i=1;i<=m;i++)
	    {
		  cin>>a[i].color;
		  a[i].num=0;
		  a[i].times=0;
	    }
	    for(i=1;i<=n;i++)
	    {
		  cin>>t>>co;
		  for(j=1;j<=m;j++)
		  {
		  	 if(a[j].color==co)
		  	 {
		  	   int number=a[j].num;
		  	   a[j].time[number]=t;
		  	   a[j].times+=t;
		  	   a[j].num++;
			 }
		  }		
	    }
	    sum=0;
	    for(i=1;i<=m;i++)//分组背包 
	    {
	    	memset(dp,0,sizeof(dp));//
	    	halftime=a[i].times/2;//halftime为理想时间,即为0-1背包的容量 
	    	for(j=0;j<a[i].num;j++) //0-1背包 
			{
	    		for(k=halftime;k>=a[i].time[j];k--)//每件衣服的时间既是花费又是价值 
	    	   dp[k]=max(dp[k],dp[k-a[i].time[j]]+a[i].time[j]);
			}	    	
			sum+=a[i].times-dp[halftime];
		}
		printf("%d\n",sum);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值