POJ3211(trie+01背包)
| 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 ;
}
POJ3211(trie+01背包)的更多相关文章
- POJ3211 Washing Clothes[DP 分解 01背包可行性]
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9707 Accepted: 3114 ...
- bnu 28890 &zoj 3689——Digging——————【要求物品次序的01背包】
Digging Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...
- poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)
题目链接: id=3211">poj3211 hdu1171 这个题目比1711难处理的是字符串怎样处理,所以我们要想办法,自然而然就要想到用结构体存储.所以最后将全部的衣服分组,然 ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 51nod1085(01背包)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...
- *HDU3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
随机推荐
- iphone越狱-------平刷回越狱前(未越狱)状态
众所周知,iPhone采用了沙盒机制,应用之间不能任意的访问,所以很多机友在拿到iPhone后,往往选择进行越狱,但是有时候,越狱的手机有的时候在安全.性能.流畅性等方面表现并不如意,所以只好寻求重新 ...
- POJ 3978(求素数)
知识点: 1.求素数的test,从2~sqrt(n): 2.假设数据非常多,能够用素数表记录,然后sum=prime[m]-prime[n]求得! ! !! !!! !! ...
- 百度 BAE 项目部署
转载:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱,我以前在上 ...
- adb命令具体解释(二)——手机缺失sqlite3时操作数据库的多种解决方式
在android应用开发无处不在SQLite数据库的身影.那么在开发中怎么使用adb命令操作数据库的功能呢? 以下我们将完整的介绍与数据库操作相关的命令集及当手机缺少sqlite3的时候的多种解决方式 ...
- 关于 ++x 和 x++ 比较难的一个例子
public class testMain { static{ int x = 5;//如果后面有static int x, 前面的定义就没有用x会被重新定义为0 } static int y; st ...
- CentOSyum操作
查看已经安装yum: yum list installed|grep mysql 查看yum: yum list|grep mysql 更新yum: rpm -ivh mysql-community- ...
- ActiveMQ 消息持久化到Mysql数据库
[root@txylucky local]# tar -zxvf apache-activemq-5.15.8-bin.tar.gz[root@txylucky local]# mv apache-a ...
- pppoe server 搭建
Ubuntu 上搭建 pppoe server sudo apt-get install pppoe $ cat /etc/ppp/pppoe-server-options # PPP options ...
- asp识别手机端
<script type="text/javascript"> var mobileAgent = new Array("iphone", &quo ...
- “ 不确定 "限制值的使用
前言 前篇文章解释了限制值的五种类型以及获取它们的方法.但是对于其中可能不确定的类型( 45类型 ),当限制值获取函数返回-1的时候,我们无法仅通过这个函数返回值-1来判断是限制值获取失败还是限制值是 ...