前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 3211 Washing Clothes

POJ 3211 Washing Clothes

作者头像
风骨散人Chiam
发布2020-10-28 10:11:18
2420
发布2020-10-28 10:11:18
举报
文章被收录于专栏:CSDN旧文

Washing Clothes

Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10738 Accepted: 3480 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 Source

POJ Monthly–2007.04.01, dearboy

题意男女朋友洗衣服,可以同时洗一种颜色的衣服,求最短时间。 对于每种颜色的衣服,就是容量为洗衣服时间总和一半的01背包问题。 代码

代码语言:javascript
复制
#include<iostream>
#include <cstdio>
#include<algorithm>
#include <cstring>
#include <map>
#include<vector>
using namespace std;
int dp[1000005];
int main(){
	int m,n;
	while(cin>>m>>n&&(m||n)){
		
vector <int >x[11];
		map<string,int>q;
		string a;int b;
    	int sum[11]={0};
		for(int i=0;i<m;i++)
			{
				cin>>a;
				q[a]=i;}
		for(int i=0;i<n;i++)
			 {	cin>>b>>a;
        			 x[q[a]].push_back(b);
        			 sum[q[a]]+=b;
				
					}
		int tot=0;
		for(int i=0;i<m;i++)
		{	
		memset(dp,0,sizeof(dp));
            for(int j=0;j<x[i].size();j++)
				for(int k=sum[i]/2;k>=x[i][j];k--)
					dp[k]=max(dp[k],dp[k-x[i][j]]+x[i][j]);
		
		tot+=(sum[i]-dp[sum[i]/2]);
		
}
		cout<<tot<<endl;
	}
	return 0;
} 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档