您的位置:首页 > 其它

杭电2639————DP之01背包第k优解

2014-07-31 19:00 453 查看


Bone Collector II

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2201 Accepted Submission(s): 1149



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

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

简单解释一下题目要求。输入给你n中物品,背包的容量(maxvolum)和一个整数k求在maxvolum的限制条件下所能得到的第k大的价值
这是一个很典型的第k优解的问题
首先看普通01背包的状态转移方程 : f[i][j] = max( f[i-1][j],f[i][j-vol[i]] + val[i])。如果要求第k优解,那么应该设定另一个状态转移方程: f[i][j][k]表示在j背包容量限制下,前i个物品所能获得的第k大价值。然后原方程就可以解释为:f[i][j]这个有序列是由f[i-1][j]和f[i-1][j-vol[i]]+val[i]这两个有序队列合并(合并的方式是通过max求取两个钟的较大的值)得到的。
有序列f[i-1][j]即f[i-1][j][1..K],f[i-1][j-vol[i]]+val[i]则理解为在f[i-1][j-vol[i]][1..K]的每个数上加上val[i]后得到的有序列。
那么f[i][j][k]就是上述两个有序列(也是通过max求两个较大的那个)合并得到

转换到代码当中去,我们需要找两个数组chose[i] 和 not_chose[i]来存储两个序列(每一个序列其实是由一系列的状态构成的)
最后将这两个数组合并即有了第k(k from  1 to k)优解的序列.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 1005
using namespace std;

struct Bone{
int vol,val;/*volum and value*/
}bone[maxn];

int main()
{
int cases;/*the number of cases*/
int n,maxvolum;/*the numbers of bones*/
int dp[maxn][35];/*dp[i][j]表示i容量下获得的第j+1大价值*/
int K;
int chose[35],not_chose[35];

scanf("%d",&cases);
while(cases--)
{
memset(dp,0,sizeof(dp));
memset(bone,0,sizeof(bone));
memset(chose,0,sizeof(chose));
memset(not_chose,0,sizeof(not_chose));
/*initialize*/
scanf("%d%d%d",&n,&maxvolum,&K);
for(int i = 0 ; i < n ; i ++)
scanf("%d",&bone[i].val);
for(int i = 0 ; i < n ; i++)
scanf("%d",&bone[i].vol);
for(int i = 0 ; i < n ; i ++ )
{
for(int j = maxvolum ; j >= bone[i].vol ; j --)
{
for(int k = 1 ; k <= K ; k ++)
{
chose[k] = dp[j - bone[i].vol][k] + bone[i].val;
/*第i件物品放入背包时,第k优解*/
not_chose[k] = dp[j][k];
/*第i件物品不放入背包时,第k优解*/
/*最后答案是这两个数组的合并,取两个数组中的较大值合成一个数组*/
}
/*开始合并*/
/*合并后的数组就是状态转移方程dp*/
int x,y,z;
x = y = z = 1;
/*这是三个下标,x,y分别表示chose和not_chose的下标,z表示第z优解*/
while( z <= K && (x <= K || y <= K))
/*循环求前K个最优解*/
/*这个地方的循环为什么是这样的????*/
{
if(chose[x]  > not_chose[y]){
dp[j][z] = chose[x];
x++;
}
else {
dp[j][z] = not_chose[y];
y++;
}
if(dp[j][z] != dp[j][z-1])
z++;
}
}
}
printf("%d\n",dp[maxvolum][K]);
}
return 0;
}
PS:学习了两天的动态规划进步实在是太小了....可以说只理解了一点点

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: