poj3211 Washing Clothes
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
这道题是分组背包的变形,先用map把每个字符串相应每个数字,然后把同样的字符串归为一类,求出最小的时间,然后累加即可了,这里每一组的最短时间能够用01背包。由于是两人同一时候做,能够知道两人的工作量相差最小时。结束这样的颜色的衣服所用的时间最少。所以能够设背包容量为sum[i]/2,然后求这个容量下的最打容量。然后这组所加的时间就是sum[i]-dp[sum[i]/2].这里还要注意一点,初始化的时候不能初始化为-1,然后将dp[0]=0,由于不一定要恰好,细致理解一下:)
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int a[15][106],num[106],sum[106],dp[100500];
int main()
{
int n,m,i,j,t,c,ans,liang,k,num1;
char s[20];
map<string,int>hash;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0)break;
memset(a,0,sizeof(a));
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
memset(s,0,sizeof(s));
hash.clear();
num1=0;
for(i=1;i<=n;i++){
scanf("%s",s);
if(hash[s]==0){
num1++;hash[s]=num1;
}
}
for(i=1;i<=m;i++){
scanf("%d%s",&c,s);
t=hash[s];
a[t][++num[t]]=c;
sum[t]+=c;
}
ans=0;
for(i=1;i<=num1;i++){
if(num[i]==0)continue;
else{
//memset(dp,-1,sizeof(dp));
memset(dp,0,sizeof(dp));
liang=sum[i]/2;
for(k=1;k<=num[i];k++){
for(j=liang;j>=a[i][k];j--){
//if(dp[j-a[i][k]]!=-1)
dp[j]=max(dp[j],dp[j-a[i][k]]+a[i][k]);
}
}
ans=ans+sum[i]-dp[liang];
}
}
printf("%d\n",ans);
}
return 0;
}
poj3211 Washing Clothes的更多相关文章
- POJ3211 Washing Clothes[DP 分解 01背包可行性]
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9707 Accepted: 3114 ...
- POJ 3211 Washing Clothes(01背包)
POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...
- POJ 3211 Washing Clothes 背包题解
本题是背包问题,可是须要转化成背包的. 由于是两个人洗衣服,那么就是说一个人仅仅须要洗一半就能够了,由于不能两个人同一时候洗一件衣服,所以就成了01背包问题了. 思路: 1 计算洗完同一颜色的衣服须要 ...
- [POJ 3211] Washing Clothes (动态规划)
题目链接:http://poj.org/problem?id=3211 题意:有M件衣服,每种衣服有一种颜色,一共有N种颜色.现在两个人洗衣服,规则是必须把这一种颜色的衣服全部洗完才能去洗下一种颜色的 ...
- POJ 3211 Washing Clothes【01背包】
题意:给出n种颜色,m件衣服,再分别给出m件衣服的颜色,和洗所需要的时间,dearboy和他的妹子一起洗衣服,且同种颜色的衣服不能同时洗,也不能两个人同时洗一件衣服,问洗完这m件衣服至少需要的时间 先 ...
- POJ 3211 (分组01背包) Washing Clothes
题意: 小明有一个贤妻良母型的女朋友,他们两个一起洗衣服. 有M种颜色的N件衣服,要求洗完一种颜色的衣服才能洗另外一种颜色. 两人可以同时洗,一件衣服只能被一个人洗. 给出洗每件衣服所用的时间,求两个 ...
- poj 3211 Washing Clothes
// 题意 :夫妻两洗衣服,衣服有m种颜色,每种颜色又有若干件,每件衣服洗完需要特定的时间,要求每种颜色放在一起洗,洗完才能洗其他衣服.最后问洗完需要的最少时间// 将衣服按颜色分类 然后求出每种颜色 ...
- poj 3211 Washing Clothes(背包)
很不错的01背包!!! 不过有点疑问!!!(注释) #include <algorithm> #include<stdio.h> #include<string.h> ...
- POJ Washing Clothes 洗衣服 (01背包,微变型)
题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...
随机推荐
- XDEBUG 远程调试
我的PHP环境是安装在虚拟机中.真机系统用的是windows.那么我要用XDEBUG调试代码,就得用XDEBUG的远程调试功能. 首先要给远程环境中安装XDEBUG扩展,具体方法:http://www ...
- 浏览器提示ERR_CONTENT_DECODING_FAILED,Gzip压缩数据无法解压
最近在页面上有个显示数据表格的功能,数据由后台传给前台JS表格插件.数据格式为JSON 由于数据量很大,就想到用GZIP压缩以后传给前台.压缩前,某个表格的数据量达到3M多,用GZIP压缩后就200K ...
- hdu6034[模拟] 2017多校1
/*hdu6034[模拟] 2017多校1*/ //暴力模拟26个26进制数即可, 要注意进位 #include<bits/stdc++.h> using namespace std; t ...
- centos的iptables设置
首先先说一下iptables是什么东西,可以简单把它理解成一个软件防火墙,一个访问控制列表,规定好哪个端口可以进来东西,哪个端口可以送出东西. 那如果不配置iptables或者iptables配置出错 ...
- 刷题总结——date(ssoj)
题目: 题目背景 SOURCE:NOIP2015-SHY-9 题目描述 小Y和小Z好不容易有机会相见啦,可是邪恶的小H却不想让他们相见.现在有一些城市,城市之间有双向路径相连,有路径相连的城市之间可以 ...
- 刷题总结——稻草人(bzoj4237cdq分治)
题目: Description JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样, ...
- wifi hand
wpa airmon-ng start wlan0airodump-ng -c 6 -w logs wlan0monaireplay-ng -0 5 -a ap'mac -c clink'mac w ...
- SyntaxError: Non-UTF-8 code starting with '\xb4'...
需在开头指定编码格式,在在最开头添加如下代码: # -*- coding: gb2312 -*- 大功告成!
- Entity Framework表名默认自动变为复数形式等常见问题解决方法
今天使用了一下手写EntityFramework,发现一些常见的问题,做个记录: 1.以前使用模板生成不太在意的问题,就是在定义实体类时,如果没映射注释,自动映射的表名会变成复数形式 如:表名==&g ...
- uva 103 Stacking Boxes(最长上升子序列)
Description Stacking Boxes Background Some concepts in Mathematics and Computer Science are simp ...