hdu-2639-01背包变形之求第k优解-Bone Collector II

本文探讨了骨收集器II问题的解决策略,包括输入格式、解题思路和代码实现。通过实例分析,详细解释了如何利用动态规划算法求解第K优解,同时提供了关键代码片段,帮助读者理解并应用该方法。

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

Bone Collector II


Problem Description
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

题意:T组数据,N个骨头,背包容量为V,求第K优解

第二行为价值,第三行为体积

分析:dp[i][j]表示体积为i时第j优解

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int V,K;
int a[35],b[35],dp[1005][35];
int c[105],w[105],d[70];
int cmp(int a1,int b1)
{
    return a1>b1;
}
void solve(int c1,int w1)
{
    int i,l,j;
    for(i=V;i>=c1;i--)
    {
        for(j=1;j<=K;j++)
        {
            a[j]=dp[i][j];      //a[]用来存不放当前物品各个体积状态下的价值
            b[j]=dp[i-c1][j]+w1;    //b[]用来存放当前物品各个体积状态下的价值
            d[j-1]=a[j];
        }
        //选取a和b数组当中的前k大,用来更新dp数组,表示体积为i时的第j优解
        for(j=1;j<=K;j++)
        {
            d[j+K-1]=b[j];
        }
        sort(d,d+2*K,cmp);
        int temp=d[0];
        dp[i][1]=temp;
        l=2;
        for(j=1;j<2*K;j++)
        {
            if(d[j]!=temp)
            {
               dp[i][l++]=d[j];
               temp=d[j];
            }
            if(l==K+1)
                break;
        }
    }
}
int main()
{
    int T,n,i,j;
    scanf("%d",&T);
    while(T--)
    {
        memset(d,0,sizeof(d));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(dp,0,sizeof(dp));
        scanf("%d%d%d",&n,&V,&K);
        for(i=0;i<n;i++)
            scanf("%d",&w[i]);
        for(i=0;i<n;i++)
            scanf("%d",&c[i]);
        for(i=0;i<n;i++)
        {
            solve(c[i],w[i]);
        }
        printf("%d\n",dp[V][K]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值