• POJ3211(trie+01背包)


    Washing Clothes
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 9384   Accepted: 2997

    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
    题意:有一堆不同颜色的衣服需要洗,为了防止不同颜色的衣服互相染色,必须洗完一种颜色的衣服再洗另一种颜色。共有两个人洗衣服,求洗完衣服所需最少的时间。
    思路:分别求洗完每种颜色的衣服所需的最少时间,可转化为01背包均分问题求解。再求其和即为答案。
    下面用map映射实现trie
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<map>
    #include<vector>
    using namespace std;
    map<string,int> trie;
    vector<int> w[11];
    int m,n;
    int dp[100005];
    int main()
    {
        while(cin>>m>>n)
        {
            if(!n&&!m)
                break;
            trie.clear();
            for(int i=0;i<m;i++)
            {
                w[i].clear();
                string color;
                cin>>color;
                trie[color]=i;
            }
            for(int i=0;i<n;i++)
            {
                int t;
                string color;
                cin>>t>>color;
                w[trie[color]].push_back(t);
            }
            int res=0;
            for(int col=0;col<m;col++)
            {
                int sum=0;
                memset(dp,0,sizeof(dp));
                for(int i=0;i<w[col].size();i++)
                    sum+=w[col][i];
                for(int i=0;i<w[col].size();i++)
                    for(int j=sum/2;j>=w[col][i];j--)
                        dp[j]=max(dp[j],dp[j-w[col][i]]+w[col][i]);
                res+=max(dp[sum/2],sum-dp[sum/2]);
            }
            cout<<res<<endl;
        }
        
        return 0;
    }
  • 相关阅读:
    RIA Animation test.
    深入浅出REST
    HTTP header中的 Cachecontrol
    Silverlight操纵html元素
    Comparing Azure storage and SQL Data Services
    SharePoint 2010 集成Window Live 认证遇到的问题 part 2
    SharePoint2010 此工作簿未存储在 Excel Services 应用程序 中受信任的位置,因此无法打开。
    SharePoint2010 使用Designer开发工作流 如何引用其他列表的查阅项
    SharePoint 2010 item级的权限控制
    Infopath 2010 如何设计虚线底框应用于合同中的输入框
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5217170.html
Copyright © 2020-2023  润新知