Washing Clothes
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 9384   Accepted: 2997

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
题意:有一堆不同颜色的衣服需要洗,为了防止不同颜色的衣服互相染色,必须洗完一种颜色的衣服再洗另一种颜色。共有两个人洗衣服,求洗完衣服所需最少的时间。
思路:分别求洗完每种颜色的衣服所需的最少时间,可转化为01背包均分问题求解。再求其和即为答案。
下面用map映射实现trie
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<vector>
using namespace std;
map<string,int> trie;
vector<int> w[];
int m,n;
int dp[];
int main()
{
while(cin>>m>>n)
{
if(!n&&!m)
break;
trie.clear();
for(int i=;i<m;i++)
{
w[i].clear();
string color;
cin>>color;
trie[color]=i;
}
for(int i=;i<n;i++)
{
int t;
string color;
cin>>t>>color;
w[trie[color]].push_back(t);
}
int res=;
for(int col=;col<m;col++)
{
int sum=;
memset(dp,,sizeof(dp));
for(int i=;i<w[col].size();i++)
sum+=w[col][i];
for(int i=;i<w[col].size();i++)
for(int j=sum/;j>=w[col][i];j--)
dp[j]=max(dp[j],dp[j-w[col][i]]+w[col][i]);
res+=max(dp[sum/],sum-dp[sum/]);
}
cout<<res<<endl;
} return ;
}

最新文章

  1. IIS8 使用FastCGI配置PHP环境支持 过程详解
  2. LeetCode Reverse String
  3. win8自动升级win8.1后 wampserver无法启动
  4. Charles是Mac的Fiddler抓包工具
  5. JSONP实例
  6. 爬虫Larbin解析(一)——Larbin配置与使用
  7. bat里如何用相对路径
  8. Linux下high CPU分析心得【非原创】
  9. Timeout for tests
  10. Windows Azure 社区新闻综述(#77 版)
  11. mac xmind快捷键
  12. GRPC: set up..
  13. HDU1285 确定比赛名次
  14. docker volume创建、备份、nfs存储
  15. linux的常用命令介绍
  16. Nginx从入门到实践(四)
  17. mysql-5.5.20预编译安装
  18. 【内核】linux2.6版本内核编译配置选项(二)
  19. windows下的Nginx+Squid+Tomcat+Memcached集群
  20. 用Head方法获得百度搜索结果的真实地址

热门文章

  1. javascript 怎么操纵OGNL标签
  2. iOS_隐藏顶部状态栏
  3. asp .net 为图片添加图片水印 .
  4. 如何获得(读取)web.xml配置文件的參数
  5. Node.js 笔记(一) nodejs、npm、express安装(转)
  6. Continuous Integration with Selenium
  7. UNIX网络编程卷1 时间获取程序client TCP 使用非堵塞connect
  8. 搭建React Native开发环境
  9. 构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(62)-EF链接串加密
  10. SQL还原数据库后,数据库显示受限制用户解决方法