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

来源:互联网 发布:cnki翻译助手软件下载 编辑:程序博客网 时间:2024/05/22 04:08

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 231).
 

Sample Input
35 10 21 2 3 4 55 4 3 2 15 10 121 2 3 4 55 4 3 2 15 10 161 2 3 4 55 4 3 2 1
 

Sample Output
1220

题意: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;}


0 0
原创粉丝点击