zoukankan      html  css  js  c++  java
  • Washing Clothes_01背包

    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=1050;
    int n,m;
    int dp[500050];
    struct node
    {
        char col[12];//衣服的颜色
        int num,sum,t[105];//数量,洗每种衣服的总时间,同一颜色的时间
    } clothe[20];
    int main()
    {
        while(~scanf("%d%d",&n,&m),n||m)
        {
            for(int i=1; i<=n; i++)
            {
                scanf("%s",clothe[i].col);
                clothe[i].num=clothe[i].sum=0;
            }
                int ti;char color[20];
                for(int i=1; i<=m; i++)
                {
                    scanf("%d%s",&ti,color);
                    for(int j=1; 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=0;
            for(int i=1; i<=n; i++)
            {
                memset(dp,0,sizeof(dp));
                int tmpsum=clothe[i].sum/2;
                for(int j=0; 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
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    SSRS_NOTE
    github快速使用指南
    Serving 404s instead of errors solved :)
    如何提高LINQtoSQL延时加载的性能
    SqlServer开发利器—SQL Prompt5
    内核通知链 学习笔记 arm
    Excel报表之js版
    JS修改Table中Td的值。
    什么时候能忙完~
    JS修改Table中Td的值(修改)
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/6010799.html
Copyright © 2011-2022 走看看