POJ 3211 Washing Clothes(01背包)@

来源:互联网 发布:手机淘宝电脑版在哪里 编辑:程序博客网 时间:2024/05/17 05:19

Washing Clothes
Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu
Submit Status

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 4red blue yellow2 red3 blue4 blue6 red0 0

Sample Output

10


因为衣服用的时间有大有小,大的可以包含小的,小的还可以累积,,所以就开总时间的一半作为背包


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <map>#include <string>#include <sstream>#include <vector>using namespace std;const int N = 100010;const int inf = 999999999;int dp[N], dp1[N];char str[N], s[N];map<string , int>p;vector<int>G[N];int main(){    int m, n, x;    while(scanf("%d %d", &m, &n),m||n)    {        p.clear();        for(int i=1;i<=m;i++)        {            G[i].clear();            scanf("%s",str);            p[str]=i;        }        memset(dp,0,sizeof(dp));        for(int i=0;i<n;i++)        {            scanf("%d %s", &x, s);            int v=p[s];            dp[v]+=x;            G[v].push_back(x);        }        int sum=0;        for(int i=1;i<=m;i++)        {            int v1=dp[i]/2;            memset(dp1,0,sizeof(dp1));            for(int k=0;k!=G[i].size();k++)            {                int v=G[i][k];                for(int j=v1;j>=v;j--)                {                    dp1[j]=max(dp1[j-v]+v,dp1[j]);                }            }            sum+=(dp[i]-dp1[v1]);        }        printf("%d\n",sum);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 员工给公司造成损失怎么办 小孩有写不完的作业家长怎么办 一年级孩子作业太粗心怎么办 孩子最近不好好做作业怎么办 工作压力大害怕做不好怎么办 孩子的数算不对怎么办? 孩子计算老是出错怎么办呢 孩子经常计算错误能怎么办 孩子老出现计算错误怎么办 孩子做作业马虎该怎么办 黑笔写错了纸破了怎么办 幼儿园报名写错怎么办名字 中考写错了字怎么办 头发没干想睡觉怎么办 突然天旋地转恶心想吐怎么办 突然天旋地转的浑身出汗怎么办 3岁半不认识数字怎么办 孩子长倒睫毛该怎么办 一年级的孩子口算慢怎么办? 大班社会领域怎么办教学反思 表带活动圈坏了怎么办 手表固针h掉了怎么办 cad图全部倒过来了怎么办 电脑打数字变粗怎么办 wps表格输入数字变乱码怎么办 文档中光标变粗怎么办 wps排序有重复的怎么办 记英语数字老是混怎么办 苹果手机输入法出数字怎么办 word文档复制过来有底色怎么办 表格复制后还再显示复制符号怎么办 笔记本字母键打出数字怎么办 wps打字字上移了怎么办 微信里黑圈里面的白字是怎么办 脖子比脸黑好多怎么办 河南许昌小学生生病办休学怎么办 1岁宝宝内向胆小怎么办 3岁宝宝内向胆小怎么办 数学物理好不喜欢学医怎么办 孩子眉毛太浓了怎么办 小娃一年级成绩太差怎么办