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. nyoj------170网络的可靠性

    网络的可靠性 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助商.它将提供先进的网络协作技术,展 ...

  2. RestSharp .net 轻量级rest客户端

    RestSharp Simple REST and HTTP API Client for .NET 官网:http://restsharp.org/ GiHub: https://github.co ...

  3. Notepad++ 配置java编译环境

    仅限于学习java或小的java程序使用.正常写代码还是eclipse吧 ---------------------分割线----------------------------- 1.配置JDK环境 ...

  4. Solr 4.3.0 配置Data import handler时出错

    启动solr的时候,居然出现了如下的错误: org.apache.solr.common.SolrException: RequestHandler init failure        at or ...

  5. Tomcat性能调优-让小猫飞奔[转]

      http://blog.csdn.net/lifetragedy/article/details/7708724   http://blog.csdn.net/lifetragedy/articl ...

  6. LInux软件的卸载和安装(转)

    在linux环境中,尤其是cenos中安装过一些软件,一般是二进制安装与源码安装,现小结一下linux中的安装与卸载. 一.通常Linux应用软件的安装包有三种: 1) tar包,如software- ...

  7. useradd/du/df/passwd/usermod命令

    一.useradd命令 useradd命令-M -u -s -g 常用 -c:加上备注文字,备注文字保存在passwd的备注栏中.  -d:指定用户登入时的启始目录. -D:变更预设值.(修改默认配置 ...

  8. bzoj 1818: [Cqoi2010]内部白点

    #include<cstdio> #include<iostream> #include<algorithm> using namespace std; struc ...

  9. JAVA传智 DAY1复习

    Java平台: Java API JVM 特点:可跨平台 Java运行机制: 编译(javac.exe)                                  运行(java.exe) J ...

  10. svn自动更新

    果对svn不熟悉,当svn上面有更新时,想看到实时效果,就得去web目录手动更新,比较麻烦 其它svn有一个自动更新的功能 利用 hook   在svn 仓库目录下面有一个hook目录 在post-c ...