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 ...
随机推荐
- 杭电acm2029-Palindromes _easy version
Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.请写一个程序判断读入的字符串是否是“回文”. Input 输入包 ...
- HTML滚动字幕代码参数详解及Js间隔滚动代码
html文字滚动代码 <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" dire ...
- SAP 调用RFC 的时候记录异常报错方式
DATA: lv_error TYPE char100. CALL FUNCTION 'ZRFC_WM_ZEL001' DESTINATION lv_desc EXPORTING process_fl ...
- 如何在启用SharePoint浏览器功能的InfoPath 表单中添加托管代码以动态地加载并显示图片
InfoPath 的浏览器表单不支持加载并显示图片,当然在模板中可以插入图片,但是如果想显示数据库的一幅图片,或是动态加载一张图片就无能为力了. 基实这个问题可以通过在浏览器表单中使用: " ...
- MapGuide Maestro 5.1发布了
MapGuide Maestro最为MapGuide开源版的authoring工具真是发展迅速,有些功能比Infrastructure Studio还给力,现在5.1版已经发布了.大家可以到http: ...
- iOS9 HTTP 通信报错解决方案
UIWebView *myview = [[UIWebView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.siz ...
- 我的Android第四章:Android的adb命令使用以及SQlite数据库运用
adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用. adb有什么用?:借助adb工具,我们可以管理设备或手机模拟器的状态.还可以进行很多手机操作, ...
- iOS之 开发常用到的宏定义
不久前做过一个小项目种用到了就记录下来方便自己以后使用,一个非常实用的宏定义来打印函数名称等 #ifdef DEBUG #define DebugLog(fmt, ...) NSLog((@" ...
- 振奋人心啊!!!!下一代.NET——ASP.NET vNext
这两天看到的.NET的新闻都好振奋人心啊!微软北美技术大会带来了好多好消息! 看到一篇博客园的文章,感觉太棒了.摘录下来.原文链接:http://news.cnblogs.com/n/208133/ ...
- js 模仿块级作用域(私有作用域)、私有变量
function outputNumbers(count){ var privateVariable = 10;//私有/局部变量,函数外部不能被访问 publicVariable = 20;//全局 ...