poj_3211

// 01背包,需要计算出每洗的种衣服所需要的最少时间
// 从总的时间/2开始枚举01背包
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAXV            100001
#define MAX_CHAR        20
#define MAX_COLOR       11
#define MAX_CLOTH       101

typedef struct _Node {
        int pos, arr[MAX_CLOTH];
}Node;

int  dp[MAXV];
Node color[MAX_COLOR];
char color_type[MAX_COLOR][MAX_CHAR], each_color[MAX_CHAR];

int get_color_index(const int &color_cnt, const char *str)
{
        for(int i = 0; i < color_cnt; i ++) {
                if( !strcmp(str, color_type[i]) ) {
                        return i;
                }
        }
}

int zero_one_knapsack(int tot, int max_v, int color_idx)
{
        memset(dp, 0, sizeof(dp));
        for(int i = 0; i < color[ color_idx ].pos; i ++) {
                for(int v = max_v; v >= color[ color_idx ].arr[i]; v --) {
                        dp[v] = max(dp[v], dp[ v-color[ color_idx ].arr[i] ]+color[ color_idx ].arr[i]);
                }
        }
        if( tot-dp[max_v] <= max_v ) {
                return dp[max_v];
        }
        return -1;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int color_cnt, cloth_cnt, need_time, idx, ans, max_v, tot;
        while( scanf("%d %d", &color_cnt, &cloth_cnt) && color_cnt && cloth_cnt ) {
                for(int i = 0; i < color_cnt; i ++) {
                        scanf("%s", color_type[i]);
                }
                ans = 0; memset(color, 0, sizeof(color));
                for(int i = 0; i < cloth_cnt; i ++) {
                        scanf("%d %s", &need_time, each_color);
                        idx = get_color_index(color_cnt, each_color);
                        color[ idx ].arr[ color[ idx ].pos ++ ] = need_time;
                }
                for(int i = 0; i < color_cnt; i ++) {
                        max_v = 0;
                        for(int d = 0; d < color[i].pos; d ++) {
                                max_v += color[i].arr[d];
                        }
                        tot = max_v, max_v >>= 1;
                        for( ; -1 == (idx = zero_one_knapsack(tot, max_v, i)); max_v ++) ;
                        ans += idx;
                }
                printf("%d\n", ans);
        }
        return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值