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. hdu----(2084)数塔(dp)

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  2. Java初学之华容道游戏

    package hhuarongdao; public class example { public static void main(String args[]) { new Hua_Rong_Ro ...

  3. lucene索引文件格式

    转自:http://blog.csdn.net/whuqin 本文介绍下lucene生成的索引有哪些文件组成,每个文件包含了什么信息.基于Lucene 4.10.0. 数据结构 索引(index)包含 ...

  4. matlab c# 混合编程

    MWArray错误: matlab 64位 vs 32位 1. visual studio没有专门的64位版.但32位版可以在64位系统上面正常使用.2.安装VS2010的时候,在安装选项里面,选择了 ...

  5. BZOJ1718 [Usaco2006 Jan] Redundant Paths 分离的路径

    给你一个无向图,问至少加几条边可以使整个图变成一个双联通分量 简单图论练习= = 先缩点,ans = (度数为1的点的个数) / 2 这不是很好想的么QAQ 然后注意位运算的优先级啊魂淡!!!你个sb ...

  6. http 报文

    HTTP报文详解 HTTP有两种报文:请求报文和响应报文,具体介绍如下 一.HTTP请求报文 先上个图,细细分析 HTTP请求报文主要包括请求行.请求头部以及请求的数据(实体)三部分 请求行(HTTP ...

  7. 将windows下的PLSQL转移到Ubuntu上

    1,首先下载安装wine,安装不成功的更新下源即可. 2,Ctal+Alt+T 打开控制台: cd ~/.wine/drive_c mkdir -p oracle/bin mkdir -p oracl ...

  8. ROS创建工作空间(三)

    查看正在使用的ROS工作空间,使用命令 echo $ROS_PACKAGE_PATH 我新建了两个

  9. Linux 常用命令杂记

    移动光标:h:向左移动j:向下移动k:向上移动l:向上移动 与window 光标移动键功能一致. 常用命令行:shift + ^ 行首shift + $ 行尾ctrl + v 可视模式 , 选择一个范 ...

  10. WP8.1 Study5:Data binding数据绑定

    一.数据绑定 最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样 ...