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背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...
随机推荐
- MySQL-MongoDB开源监控利器之PMM
背景说明: PMM是percona公司提供的一个对于MySQL和MongoDB的监控和管理平台.PMM有两部分组成PMM Client和PMM Server PMM Client:安装在每一台需要进行 ...
- socket编程-微软小兵
socket两端建立连接,不断开的连接的情况下做数据交互,客户端发送数据和服务端返回数据.直到客户端要求断开,则关闭连接. 代码目录结构:
- Django AUTHENTICATION_BACKENDS
指定认证后端 Django维护一个”authentication backends”的列表用来测试认证.当调用 django.contrib.auth.authenticate() — Django将 ...
- hdu 2857 点在直线上的投影+直线的交点
Mirror and Light Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence(poj 1019)
题意: 有一串数字串,其规律为 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 1234 ...
- Filter里面实现未登录跳转,已登录权限判断
package com.erichfund.cljjfof.server.util; import java.io.IOException; /** * @author 作者 zhuzhengquan ...
- 创建 Image
本节演示如何通过 Web GUI 和 CLI 两种方法创建 Image. OpenStack 为终端用户提供了 Web UI(Horizon)和命令行 CLI 两种交换界面.两种方式我们都要会用. 可 ...
- postgresql 10 分页
示例: select * from test limit 2 offset 2; limit:指查多少条数据 offset:从下标多少开始查,下标从0开始,不能为负数. offset计算公式: var ...
- AC日记——圆桌聚餐 cogs 729
729. [网络流24题] 圆桌聚餐 ★★ 输入文件:roundtable.in 输出文件:roundtable.out 评测插件时间限制:1 s 内存限制:128 MB «问题描述: ...
- 解决 ecshop 搜索特殊字符关键字(如:*,+,/)导致搜索结果乱码问题
病症:ecshop系统搜索会对搜索关键字进行分词,然后对关键字分词进行正则匹配,并且标红加粗处理,如果关键字分词有特殊字符,则正则匹配结果会导致乱码 解决方法: 1.找到特殊字符串数组:$ts_str ...