POJ3211 Washing Clothes[DP 分解 01背包可行性]
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 9707 | Accepted: 3114 |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int N=,M=,T=1e5+;
int m,n,num=,cnt[M],ans=;
string s;
map<string,int> mp;
struct clo{
int t,col;
}a[N];
int f[T];
void dp(){
for(int c=;c<=m;c++){
int sum=cnt[c];
memset(f,,sizeof(f));
for(int i=;i<=n;i++) if(a[i].col==c)
for(int j=sum/;j>=a[i].t;j--)
f[j]=max(f[j],f[j-a[i].t]+a[i].t);
ans+=cnt[c]-f[sum/];
}
}
void init(){
mp.clear();
num=ans=;
memset(cnt,,sizeof(cnt));
}
int main(){
while(true){
scanf("%d%d",&m,&n);
if(m==&&n==) break;
init();
for(int i=;i<=m;i++){
cin>>s;
mp[s]=++num;
}
for(int i=;i<=n;i++){
scanf("%d",&a[i].t);
cin>>s;
a[i].col=mp[s];
cnt[a[i].col]+=a[i].t;
}
dp();
printf("%d\n",ans);
}
}
POJ3211 Washing Clothes[DP 分解 01背包可行性]的更多相关文章
- poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)
题目链接: id=3211">poj3211 hdu1171 这个题目比1711难处理的是字符串怎样处理,所以我们要想办法,自然而然就要想到用结构体存储.所以最后将全部的衣服分组,然 ...
- poj3211 Washing Clothes
Description Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a ...
- HDU2191--多重背包(二进制分解+01背包)
悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- [HDOJ5543]Pick The Sticks(DP,01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
- hdu1203 I NEED A OFFER!---概率DP(01背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材 ...
- poj1742(多重背包分解+01背包二进制优化)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- HDU 2955 Robberies(概率DP,01背包)题解
题意:给出规定的最高被抓概率m,银行数量n,然后给出每个银行被抓概率和钱,问你不超过m最多能拿多少钱 思路:一道好像能直接01背包的题,但是有些不同.按照以往的逻辑,dp[i]都是代表i代价能拿的最高 ...
- DP入门——01背包 & 完全背包
01背包: 采药: https://www.luogu.org/problemnew/show/P1048 #include <iostream> #include <algorit ...
随机推荐
- JavaScript学习笔记-JSON对象
JSON 是一种用来序列化对象.数组.数值.字符串.布尔值和 null 的语法.它基于 JavaScript 语法,但是又有区别:一些 JavaScript 值不是 JSON,而某些 JSON 不是 ...
- 用SVG绕过浏览器XSS审计
[Translated From]:http://insert-script.blogspot.com/2014/02/svg-fun-time-firefox-svg-vector.html === ...
- 部署基于国际版Azure的SharePoint三层架构服务器场
前言 微软Azure国际版已经很普及了,这里没有用国内版(世纪互联),用的是国际版,当然是由于公司性质的缘故.这里一步步图文的方式,分享给大家创建Azure国际版的SharePoint三层架构的过程, ...
- 如何在Infraworks中创建多树种组成的森林
在Infraworks 2014中,你可以有shp文件导入生成树木和森林,也可以直接在模型中规划一片区域作为森林.美中不足的就是,这些充其量叫树林不能叫森林,因为他们的样式都是一个树种,而真正的森林肯 ...
- iOS 检查更新
注意:苹果官方是不允许app具有检查更新提示! //直接跳转到AppStore - (void)setUpAppUpdate { [ServerData queryGetURL:@{@" ...
- IOS动画
基础动画 //创建基础动画 CABasicAnimation * basic = [CABasicAnimation animation]; //动画路径 basic.keyPath ...
- 【原】iOS设计模式之:建造者模式Builder Pattern,用于改进初始化参数
本文主要讨论一下iOS中的Builder Pattern.与网上很多版本不同,本文不去长篇大论地解释建造者模式的概念,那些东西太虚了.设计模式这种东西是为了解决实际问题的,不能为了设计模式而设计模式, ...
- ThinkPHP的RBAC原理分析
转载 http://jingyan.baidu.com/article/a948d65151a4810a2ccd2e60.html 方法/步骤 多用户对节点的操作的权限控制,这句话,经过反复扩展功能, ...
- 心理控制方法——阅读Notes
1.自助式情感手术 祛除自我意象中的伤疤的要点 2. 你制造错误,但是错误不应造就你 你身上的缺点不是你的错 3. 不仅要原谅别人,也要原谅自己 4. 怨恨是一条通向失败的道路 5. 注意来 ...
- WebServer中异步操作的一些总结
1.异步操作本身不会改善IO的性能 2.当任务多为IO操作时普通的工作线程将会减少,使CPU对工作线程的维护降低,从而提高CPU对其它任务的利用率 3.如果专用的IO线程,需要执行的专用任务较多时,专 ...