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. Java 集合系列 16 HashSet

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  2. oracle用户

    如果要了解oracle中用户信息,可以查询数据字典dba_users.在sql*plus中,使用system用户登录,查询语句如下: select username,account_status fr ...

  3. [工程备案]linux基本命令以及C和C++编程

    whereis 查看程序安装在了哪里 where 产看运行程序在哪里. libtool --mode=link  g++ test.cpp  -o test  libSegmentorForSim2T ...

  4. java面向对象编程——第二章 java基础语法

    第二章 java基础语法 1. java关键字 abstract boolean break byte case catch char class const continue default do ...

  5. iOS8 针对开发者所拥有的新特性汇总如下

    iOS8 针对开发者所拥有的新特性汇总如下 1.支持第三方键盘 2.自带网页翻译功能(即在线翻译) 3.指纹识别功能开放:第三方软件可以调用 4.Safari浏览器可直接添加新的插件. 5.可以把一个 ...

  6. flash as3 socket安全服务网关(socket policy file server)

    关键字: SecurityErrorEvent socket as3 flash有着自己的一套安全处理模式,在socket方面,我这样的菜鸟无法理解他的好处:一句话,不怀好意的人如果想用flash写一 ...

  7. UVa 11021 - Tribles

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. IT公司100题-17-第一个只出现一次的字符

    问题描述: 在一个字符串中找到第一个只出现一次的字符.例如输入asdertrtdsaf,输出e.   分析: 最简单的方法是直接遍历,时间复杂度为O(n^2). 进一步思考: 字符串中的字符,只有25 ...

  9. 【C语言学习】-01 C基础

    本文目录: 0.进制转换 1.C数据类型 2.常量变量 3.运算符 4.表达式 5.格式化输入输出 回到顶部 0.进制转换 在计算机中存储的数据,主要是以二进制形式存在,而我们生活中主要使用的有十进制 ...

  10. Mvc学习--1

    1.缓存机制[OutputCache(Duration=10)] 后面的 duration 表示缓存时间 直接放在action上面 是一个特性2.文件上传 @using (Html.BeginForm ...