poj-3211(01背包)

本文介绍了一个基于01背包问题的算法,用于解决情侣二人如何高效分配不同颜色衣物的清洗任务,以达到最短总清洗时间的目标。通过计算每种颜色衣物的最优清洗时间分配,实现整体清洗效率的最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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背包;

就是就算在所有时间中找出和最接近 sum / 2 的最大值;

递推式:dp[i] = dp[i] + dp[i - v[i]] + v[i];

初始值都为0;

AC代码:

#include<iostream>
#include<cstdio>
#include<memory.h>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#include<memory.h>
using namespace std;
typedef long long ll;
const int N=1000+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int num[20][110],dp[100010],num1[20],sum[20];
int main()
{
    int n,k,i,j,h,ans;
    char color[20][20],nc[20];
    while(scanf("%d%d",&k,&n)>0)
    {
        if(k==0&&n==0) break;
        memset(num,0,sizeof(num));
        memset(num1,0,sizeof(num1));
        memset(sum,0,sizeof(sum));ans=0;
        for(i=1;i<=k;i++)
            cin>>color[i];
        for(i=1;i<=n;i++)
        {
            int x;
            cin>>x>>nc;
            for(j=1;j<=k;j++) if(!strcmp(nc,color[j])) break;
            num[j][num1[j]++]=x;
            sum[j]+=x;
        }
        for(i=1;i<=k;i++)
        {
            memset(dp,0,sizeof(dp));
            for(j=0;j<num1[i];j++)
            {
                for(h=sum[i]/2;h>=num[i][j];h--)
                    dp[h]=max(dp[h],dp[h-num[i][j]]+num[i][j]);
            }
            ans+=(sum[i]-dp[sum[i]/2]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间16ms,还是很快的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值