hdu 2639 01 背包 存在k 个多解问题

来源:互联网 发布:思维 知乎 编辑:程序博客网 时间:2024/06/08 01:23


题目地址

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

Bone Collector II

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4693    Accepted Submission(s): 2443


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

题目大意 给 背包总量 , 物品价值以及 物品的重量,  和 想求的第k 个优解问题,    总的来说 仍是 01 背包,

我们仍可以安装01背包的思路来, 但是 要保存 k 解问题,  即 01 背包 一维数组解最优的问题, 那么我们就可以 用一个二维数组, 来存,  用行来表示背包的容量, 用列表示

第几个k解问题  将结果存到二维数组中, 即可

思路:

1.用两个数组, 分别存 物品放与不放的结果,

2. 扫描 全部的结果, 将二者进行比较   依次放入,  则当 全部扫描完后,  则最终的结果 就是在当前 最开始 第一个for 循环 条件下 的最优解

3. 整个for 循环, 结束  将每次的最优解都存在这个二维数组当中,  就可以 根据 要求 直接输出 结果就可以

#include <stdio.h>#include <iostream>#include <cstring>#include <algorithm>#define max(a,b) ((a)>(b)?(a):(b))#define N 1010using namespace std;int we[N];int v[N];int a[N];int b[N];int dp[N][100];int main(){    int T,n,m,k,i,j,l,x,y;    while(~scanf("%d",&T))    {        while(T--)        {            memset(dp,0,sizeof(dp));            memset(we,0,sizeof(we));            memset(v,0,sizeof(v));            cin>>n>>m>>k;            for(i=0;i<n;i++)                cin>>v[i];            for(i=0;i<n;i++)                cin>>we[i];            for(i=0;i<n;i++)            {                for(j=m;j>=we[i];j--)                {                    for(l=0;l<k;l++)// 先把每一种的放不放的结果 存下来                    {                        a[l]=dp[j-we[i]][l]+v[i];  //放                        b[l]=dp[j][l];//不放                    }                    a[l]=b[l]=-1;// 目的是  作为结束条件 没有结束条件,wrong  会结束不了                     x=y=0;                    for(int num=0;num<k&&(x<k||y<k);)                    {                        dp[j][num]=a[x]>=b[y]? a[x++]:b[y++];//                        cout<<dp[j][num]<<endl;                        if(dp[j][num]!=dp[j][num-1])                                num++;                    }                }            }//            for(i=0;i<=m;i++)//            {//                for(j=0;j<=k;j++)//                    cout<<dp[i][j]<<" ";//                cout<<endl;//            }            cout<<dp[m][k-1]<<endl;        }    }    return 0;}


0 0
原创粉丝点击