• POJ3211 Washing Clothes[01背包问题]


    Washing Clothes
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 7063   Accepted: 2089

    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

    Source

     
     
     
    感觉这个题目挺不错的!虽然自己还是没有做出来。
    code:
     1 #include <iostream>   
     2 #include <iomanip>   
     3 #include <fstream>   
     4 #include <sstream>   
     5 #include <algorithm>   
     6 #include <string>   
     7 #include <set>   
     8 #include <utility>   
     9 #include <queue>   
    10 #include <stack>   
    11 #include <list>   
    12 #include <vector>   
    13 #include <cstdio>   
    14 #include <cstdlib>   
    15 #include <cstring>   
    16 #include <cmath>   
    17 #include <ctime>   
    18 #include <ctype.h> 
    19 using namespace std;
    20 
    21 int n,m;                      //m表示衣服的颜色种类,n表示衣服的件数 
    22 int dp[10001];                //1000*10+1  
    23 
    24 struct clothes                //某一种颜色的结构体 
    25 {
    26     int num[1001],cnt,sum;    //num表示洗每一件这种颜色的衣服的时间,cnt表示有几件衣服,sum表示总时间 
    27     char type[26];            // type表示这种衣服的颜色 
    28 }clo[26];
    29 
    30 int main()
    31 {
    32     int i,j,k;
    33     char temp[26];
    34     int num;
    35     int min;
    36     int ans;
    37     while(~scanf("%d%d",&m,&n),(m||n))
    38     {
    39         memset(clo,0,sizeof(clo));
    40         for(i=0;i<m;i++)
    41             scanf("%s",clo[i].type);
    42         for(i=0;i<n;i++)
    43         {
    44             scanf("%d%s",&num,temp);
    45             for(j=0;j<m;j++)
    46             {
    47                 if(!strcmp(temp,clo[j].type))
    48                  {
    49                      clo[j].num[clo[j].cnt++]=num;     //洗第i种颜色第j件衣服的时间 
    50                     clo[j].sum+=num;                  //第i种颜色衣服总共所需花的时间 
    51                 }
    52             }
    53         } 
    54 //--------------------------------DP--------------------------------------------------------------------------- 
    55         ans=0;
    56         for(i=0;i<m;i++)                                 //第i种颜色的衣服 
    57         {
    58             memset(dp,0,sizeof(dp));
    59             for(j=0;j<clo[i].cnt;j++)                       //一次取第i种颜色的衣服进行判断 
    60                {
    61                 for(k=clo[i].sum;k>=clo[i].num[j];k--)          //01背包从最大空间开始递减 
    62                 {
    63                        if(dp[k-clo[i].num[j]]+clo[i].num[j]>dp[k])
    64                            dp[k]=dp[k-clo[i].num[j]]+clo[i].num[j];
    65                    }
    66             }
    67             ans+=clo[i].sum-dp[clo[i].sum/2];
    68        }
    69 //-------------------------------DP-------------------------------------------------------------------------- 
    70        printf("%d\n",ans);
    71     }
    72     return 0;
    73 } 
    74 
    75 
    76 //注意数组的大小,WA了一次 






                If you have any questions about this article, welcome to leave a message on the message board.



    Brad(Bowen) Xu
    E-Mail : maxxbw1992@gmail.com


  • 相关阅读:
    最长回文子串 leetcode
    leetcode Plus one
    n的阶乘末尾有几个0?
    求两数的最大公约数和最小公倍数
    汉诺塔
    求n的阶乘
    svn book
    求斐波那契数列第n项
    判断一个数是否是素数
    <C Traps and Pitfalls>笔记
  • 原文地址:https://www.cnblogs.com/XBWer/p/2599466.html
Copyright © 2020-2023  润新知