HDU 2639 Bone Collector II————01背包求第k优解

HDU 2639:http://acm.hdu.edu.cn/showproblem.php?pid=2639

 

Bone Collector II

The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link:

Here is the link: http://acm.hdu.edu.cn/showproblem.php?pid=2602

Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.

If the total number of different values is less than K,just ouput 0.

Input

The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, K(N <= 100 , V <= 1000 , K <= 30)representing the number of bones and the volume of his bag and the K we need. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output

One integer per line representing the K-th maximum of the total value (this number will be less than 2的31次幂).

Sample Input

3
5 10 2
1 2 3 4 5
5 4 3 2 1
5 10 12
1 2 3 4 5
5 4 3 2 1
5 10 16
1 2 3 4 5
5 4 3 2 1

Sample Output

12
2
0

 

思路:

这道题是在经典01背包问题的基础上的变形,01背包问题是求最优解,这里要求第k优的解,掌握经典01背包问题后,这里理解起来也会比较简单。

经典01背包问题,只需建立二维dp数组,其中dp[i][j]存放的是考虑到第i个物品,背包容量为j的背包能放的最大价值。先在,为了求出第k优的解,我们可以让这个dp数组中存的不在是一个数,而是一个序列,这个序列存的是第1~k大的价值,也就是让dp增加一个维度,dp[i][j][k]。计算方法就是,整体依然是求解01背包的框架,但不再是dp[i][j] = max(dp[i-1][j-c[i]]+w[i], dp[i-1][j])。为了求解dp[i][j]这个序列,将 取这个物品的情况 和 不取这个物品的情况 这两个序列合并起来,

不取这个物品的情况很简单,就是   dp[i-1] [j] [1] ~~~ dp[i-1] [j] [k]   这个序列,

取这个物品的情况,得把这个序列上的所有数加上b[i],就是   dp[i-1] [j-c[i]] [1] ~~~ dp[i-1] [j-c[i]] [k]   所有数加上 b[i],

然后这两个序列合并后,取前面大的1~k个数,就是需要的序列。

同样的,也可以像经典01背包问题一样,将dp降一个维度。

 

#include<cstdio>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>

#define MAXM 1005
#define MAXK 35
#define MAXN 105

using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, v, k;
        int i, j, kk, dp[MAXM][MAXK], vel[MAXN], w[MAXN];
        memset(dp, 0, sizeof(dp));
        scanf("%d%d%d", &n, &v, &k);
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &vel[i]);
        }
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &w[i]);
        }
        for(i = 1; i <= n; i++)
        {
            for(j = v; j >= w[i]; j--)
            {
                int a[MAXK], b[MAXK];
                int x, y, z;
                for(kk = 1; kk <= k; kk++)
                {
                    a[kk] = dp[j][kk];
                    b[kk] = dp[j-w[i]][kk] + vel[i];
                }
                a[kk] = -1;
                b[kk] = -1;
                x = y = z = 1;
                while(z <= k && (a[x] != -1 || b[y] != -1))
                {
                    if(a[x] > b[y])
                    {
                        dp[j][z] = a[x];
                        x++;
                    }
                    else
                    {
                        dp[j][z] = b[y];
                        y++;
                    }
                    if(dp[j][z] != dp[j][z-1])
                    {
                        z++;
                    }
                }
            }
        }
        printf("%d\n", dp[v][k]);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值