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

【题意】给出n,m,n表示n种颜色,m表示m件衣服,给出每件衣服的时间和颜色。

【思路】先求出洗每种颜色的衣服所用的总时间,为了让两个人所用时间尽可能相同,把总时间的一半当做背包容量,每件衣服所花费的时间既是物体体积,又是物品价值,这样就转化为01背包,求背包的最大价值,然后用这种颜色的总时间减去最大价值就是洗这种颜色的衣服所用的最短时间。

参考:http://blog.csdn.net/lyhvoyage/article/details/17041907

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int N=;
int n,m;
int dp[];
struct node
{
char col[];//衣服的颜色
int num,sum,t[];//数量,洗每种衣服的总时间,同一颜色的时间
} clothe[];
int main()
{
while(~scanf("%d%d",&n,&m),n||m)
{
for(int i=; i<=n; i++)
{
scanf("%s",clothe[i].col);
clothe[i].num=clothe[i].sum=;
}
int ti;char color[];
for(int i=; i<=m; i++)
{
scanf("%d%s",&ti,color);
for(int j=; j<=n; j++)
{
if(!strcmp(color,clothe[j].col))//颜色相同
{
int tmp=clothe[j].num;
clothe[j].t[tmp]=ti;
clothe[j].sum+=ti;
clothe[j].num++;
} }
} int ans=;
for(int i=; i<=n; i++)
{
memset(dp,,sizeof(dp));
int tmpsum=clothe[i].sum/;
for(int j=; j<clothe[i].num; j++)
{
for(int k=tmpsum; k>=clothe[i].t[j]; k--)
{
dp[k]=max(dp[k],dp[k-clothe[i].t[j]]+clothe[i].t[j]);
}
}
ans+=clothe[i].sum-dp[tmpsum];
}
printf("%d\n",ans);
}
return ;
}

Washing Clothes_01背包的更多相关文章

  1. poj 3211 Washing Clothes(背包)

    很不错的01背包!!! 不过有点疑问!!!(注释) #include <algorithm> #include<stdio.h> #include<string.h> ...

  2. POJ 3211 Washing Clothes 背包题解

    本题是背包问题,可是须要转化成背包的. 由于是两个人洗衣服,那么就是说一个人仅仅须要洗一半就能够了,由于不能两个人同一时候洗一件衣服,所以就成了01背包问题了. 思路: 1 计算洗完同一颜色的衣服须要 ...

  3. POJ3211 Washing Clothes[DP 分解 01背包可行性]

    Washing Clothes Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9707   Accepted: 3114 ...

  4. POJ 3211 Washing Cloths(01背包变形)

    Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...

  5. POJ 3211 Washing Clothes(01背包)

    POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...

  6. POJ 3211 Washing Clothes【01背包】

    题意:给出n种颜色,m件衣服,再分别给出m件衣服的颜色,和洗所需要的时间,dearboy和他的妹子一起洗衣服,且同种颜色的衣服不能同时洗,也不能两个人同时洗一件衣服,问洗完这m件衣服至少需要的时间 先 ...

  7. POJ 3211 (分组01背包) Washing Clothes

    题意: 小明有一个贤妻良母型的女朋友,他们两个一起洗衣服. 有M种颜色的N件衣服,要求洗完一种颜色的衣服才能洗另外一种颜色. 两人可以同时洗,一件衣服只能被一个人洗. 给出洗每件衣服所用的时间,求两个 ...

  8. POJ Washing Clothes 洗衣服 (01背包,微变型)

    题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...

  9. POJ 3211 Washing Clothes 0-1背包

    题目大意: xxx很懒,但他有个漂亮又勤奋的女友 (尼玛能不能不刺激我,刚看到这题的时候发现自己的衣服没洗!!!) 可以帮他洗衣服. 洗衣服的时候要求不同的颜色的衣服不能同时洗.一人洗一件的话,问最短 ...

随机推荐

  1. mysql的小知识点(关于数据库的导入导出 对于windows)

    对于,一个存在的数据,我们该如何去打包成.sql属性的文件呢? 直接进行这两条语句: D:\Program Files\MySQL\mysql\bin>mysqldump -u root -p ...

  2. webBrowser1执行函数

    IHTMLWindow2 Win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;string s = "alert('x');& ...

  3. 输出有序数组的中两个元素差值为指定值diff的两个元素

    题目: 输出有序数组的中两个元素差值为指定值diff的两个元素. 思路: 这与输出两个元素的和的值为一定值类似,需要两个指针,不同的是:指针不是一左一右,而是一前一后. 如果差值等于diff,则返回: ...

  4. content.boundingRectWithSize计算出来的高度不准

      计算出来的高度会少一行的高度,最后一行会显示不全.减掉padding会解决这个问题.   let padding = self.reviewText.textContainer.lineFragm ...

  5. Html5工具

    HTML5被看做是web开发者创建流行web应用的利器,增加了对视频和Canvas 2D的支持.用HTML5的优点主要在于,这个技术可以进行跨平台的使用.比如你开发了一款HTML5的游戏,你可以很轻易 ...

  6. eclipse-mysql-tomcat bug之旅

    赶紧默念三遍google大法好... [连接数据库 servlet调用提示找不到可加载的driver,普通的.java文件没问题] 表示不服啊...明明可以连上啊...为什么多了几个中间界面就不好使了 ...

  7. TextBoxButton控件的开发实现

    效果图: 实现代码: public TextBoxButton() { _button = new Button { ForeColor = System.Drawing.SystemColors.G ...

  8. 12个强大的Chrome插件

    Chrome功能强大,也得益于其拥有丰富的扩展资源库.Chrome Web Store里有各种各样的插件,可以满足你使用Chrome时的各种要求.和Firefox一样,Chrome的扩展非常容易安装, ...

  9. lucene 过滤结果

    package cn.itcast.h_filter; import java.util.ArrayList; import java.util.List; import org.apache.luc ...

  10. IT公司100题-8-智力题

    问题1: 有两个房间,一间房里有三盏灯,另一间房有控制着三盏灯的三个开关, 这两个房间是分割开的,从一间里不能看到另一间的情况. 现在要求受训者分别进这两房间一次,然后判断出这三盏灯分别是由哪个开关控 ...