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. 创建PO/SO

    IF P_ZY EQ 'X'."直营订单 调拨单 PERFORM FRM_INIT_PO_HEADER. PERFORM FRM_INIT_PO_ITEM. PERFORM FRM_INIT ...

  2. Gliffy

    http://www.gliffy.com Gliffy 支持在线制作流程图,能够很好的支持中文,基础版本免费.在线制作的思维导图是公开的,高级版本有设置隐私权的权力.可以嵌入博客,办公室应用软件中, ...

  3. Q: How could I use MATLAB interface for parameter selection?

    Q: How could I use MATLAB interface for parameter selection? One can do this by a simple loop. See t ...

  4. for循环练习——7月23日

    练习一:输入一个整数,求从1到这个数的累加和 //练习1:输入一个整数,计算从1加到这个数的结果 Console.Write("请输入一个正整数:"); int a = int.P ...

  5. 5月25日 DOM

    练习一:输入答案,弹出是否正确. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  6. Generic泛型

    1.问题 未使用泛型时,元素的类型不安全:操作麻烦,可能需要强制转换import java.util.ArrayList;import java.util.List;import org.junit. ...

  7. 转载:javascript面向对象编程:非构造函数的继承

    一.什么是"非构造函数"的继承? 比如,现在有一个对象,叫做"中国人". var Chinese = { nation:'中国' }; 还有一个对象,叫做&qu ...

  8. maven的安装与使用

    一.Maven是什么 Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object model (POM)概念来管理项目,所有的项目配置信息都被定义 ...

  9. FZU 2032 Log函数问题 模拟小数加法

    题目链接:Log函数问题 2 / 49 Problem G FZU 2032 Log函数问题 不知道为什么...比赛时高精度难倒了一票人...成功搞出大新闻... 试了一下直接double相加超时,然 ...

  10. MySql 分组排序取时间最大的一条记录

    SELECT A.* FROM digital_asset A, (SELECT name, max(last_updated) max_day FROM digital_asset GROUP BY ...