hdu 2639Bone Collector II 背包的第K优解问题


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

第K优解问题

其基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并。如果要求第K优解,那么状态f[i][v]就应该是一个大小为K的数组f[i][v][1..K]。其中f[i][v][k]表示前i个物品、背包大小为v时,第k优解的值。 显然f[i][v] [1..K]这K个数是由大到小排列的,所以我们把它认为是一个有序队列。
然后原方程就可以解释为:f[i][v]这个有序队列是由f[i-1][v]和f[i-1][v-c[i]]+w[i]这两个有序队列合并得到的。有序队列 f[i-1][v]即f[i-1][v][1..K],f[i-1][v-c[i]]+w[i]则理解为在f[i-1][v-c[i]][1..K]的每个数上加上w[i]后得到的有序队列。合并这两个有序队列并将结果的前K项储存到f[i][v][1..K]中的复杂度是O(K)。最后的答案是f[N] [V][K]。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int bag[1000+10][55],val[101],n,v,k,vol[102],used[1000+10],a[55],b[55];
void _01bag()
{
	int i,j,t,x,y,z;
	memset(used,0,sizeof(used));
	memset(bag,0,sizeof(bag));
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
    for(i=0;i<n;i++)
		for(j=v;j>=vol[i];j--)
		{
			for(t=1;t<=k;t++)
			{
				a[t]=bag[j][t];
				b[t]=bag[j-vol[i]][t]+val[i];
			}
			x=y=z=1;
			while(z<=k&&(x<=k||y<=k))
			{
				if(a[x]>=b[y])
				{
					bag[j][z]=a[x]; x++;
				}
				else 
				{
					bag[j][z]=b[y];y++;
				}

				if(bag[j][z]!=bag[j][z-1]) z++;
			}
		}
		printf("%d\n",bag[v][k]);	
}
int main()
{
    int i,cas;
    scanf("%d",&cas);
	
	while(cas--)
	{
		scanf("%d %d %d",&n,&v,&k);
		for(i=0;i<n;i++)
            scanf("%d",&val[i]);
		for(i=0;i<n;i++)
            scanf("%d",&vol[i]);
		_01bag(); 	
	}   
	return 0;
} 


参考了宇宙吾心

 
 

你可能感兴趣的:(hdu 2639Bone Collector II 背包的第K优解问题)