poj3211分组背包

  Current Contest
Past Contests
Scheduled Contests
Award Contest
sdau20171754      Log Out
Mail:4(1)
Login Log      Archive

Language:Default

Washing Clothes

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions:10691   Accepted: 3462

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

扫描二维码关注公众号,回复: 5840118 查看本文章
10

Source

POJ Monthly--2007.04.01, dearboy

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#define maxn 20
#define maxm 105
#define maxx 10005
using namespace std;
int time[maxn][maxm];
int num[maxn];
int dp[maxx];
map<string,int>mp;
int m,n;
int main()
{
    while(cin>>m>>n)
    {if(n==0&&m==0)
    break;
        string s;
        for(int i=1;i<=m;i++)
        {
            cin>>s;
            mp[s]=i;
        }
        memset(num,0,sizeof(num));
            for(int i=1;i<=n;i++)
            {
                int w;
                string str;
                cin>>w>>str;
                int u=mp[str];
                time[u][++num[u]]=w;
            }
            int ans=0;
            for(int i=1;i<=m;i++)
            {int sum=0;
            memset(dp,0,sizeof(dp));
                for(int j=1;j<=num[i];j++)
                sum+=time[i][j];
                for(int j=1;j<=num[i];j++)
                for(int k=sum/2;k>=time[i][j];k--)
                dp[k]=max(dp[k],dp[k-time[i][j]]+time[i][j]);
                ans+=sum-dp[sum/2];
            }
           cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/89072923
今日推荐