再谈01背包--01背包第k优解

普通的01背包如果已经搞得很明白的话,这个应该很好懂。。具体原理已经附在代码开头,这里就不说了。。

Type1:直接求第k优解的裸题。。HDU2639

Bone Collector II

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


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优解的题目。。。
/***************************************************
* author:sgx
* date:2013/09/19
* problem:01背包第k优解
* 分析:第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]。
* hint:和普通01背包是相同的,也可以降维。。
***************************************************/
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

const int maxn=1001;

int dp[maxn][33];//dp[i][j]表示i体积下第j优解
int c[maxn],w[maxn];

inline int max(int a,int b)
{
    return a>b?a:b;
}
int main()
{
     int T;
     scanf("%d",&T);
     while(T--)
     {
        int n,m,i,j,k,p,a[33],b[33];
            
	 scanf("%d%d%d",&n,&m,&k);
         for(i=0;i<n;i++)
             scanf("%d",&w[i]);
         for(i=0;i<n;i++)
             scanf("%d",&c[i]);
         memset(dp,0,sizeof(dp));
         for(i=0;i<n;i++)
         {
             for(j=m;j>=c[i];j--)
             {
                 for(p=1;p<=k;p++)
                 {
                     a[p]=dp[j-c[i]][p]+w[i]; //在dp[i]=max(dp[j],dp[j-c[i]]+w[i])中产生后续优解,小的为次优
                     b[p]=dp[j][p];
                 }
                 a[p]=b[p]=-1;
                 int x,y,z;
                 x=y=z=1;
                 while(z<=k&&(a[x]!=-1||b[y]!=-1))//产生分歧后找到其中最大的k的保留
                 {
                     if(a[x]>b[y])
                     {
                         dp[j][z]=a[x];
                             x++;
                     }
                     else
                     {
                         dp[j][z]=b[y];
                             y++;
                     }
                     if(dp[j][z]!=dp[j][z-1])//去掉重复了的优解
                        z++;
                 }
             }
         }
         printf("%d\n",dp[m][k]);
     }
     return 0;
}

Type2:简单变化,求前k优解。。。

Problem 1259 - 多人背包
Time Limit: 2000MS      Memory Limit: 65536KB      Difficulty:    
Total Submit: 65     Accepted: 16     Special Judge: No 
Description
DD 和好朋友们要去爬山啦!他们一共有 K 个人,每个人都会背一个包。这些包的容量是相同的,都是 V。可以装进背包里的一共有 N 种物品,每种物品都有给定的体积和价值。
在 DD 看来,合理的背包安排方案是这样的:
每个人背包里装的物品的总体积恰等于包的容量。
每个包里的每种物品最多只有一件,但两个不同的包中可以存在相同的物品。
任意两个人,他们包里的物品清单不能完全相同。
在满足以上要求的前提下,所有包里的所有物品的总价值最大是多少呢?
总人数 K<=50。
每个背包的容量 V<=5000。
物品种类数 N<=200。
其它正整数都不超过 5000。
Input
第一行有三个整数:K、V、N。
第二行开始的 N 行,每行有两个整数,分别代表这件物品的体积和价值。
Output
只需输出一个整数,即在满足以上要求的前提下所有物品的总价值的最大值。
Sample Input
2 10 5
3 12
7 20
2 4
5 6
1 1
Sample Output
57
Hint

一种可以得到最优解的方案是:第一个人背体积为 7、2、1 的三种物品,价值为 25。第二个人背体积为 3、7 的两种物品,价值为 32。总价值 57。

/***************************************************
* author:sgx
* date:2013/09/19
* problem:01背包前k优解
***************************************************/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=5000+5;

int a[maxn],b[maxn];
int dp[maxn][50];

int n,k,v;


struct Goods
{
     int a, b;
     bool operator < (const Goods& r) const
     {
        return a < r.a;
     }
} g[maxn];


inline void debug()
{
     printf("where is wrong?\n");
}

inline int max(int a,int b)
{
    return a>b?a:b;
}

inline int min(int a,int b)
{
    return a<b?a:b;
}

inline void solve()
{
     int x,y,z,i,j,t;
     memset(dp,0,sizeof(dp));
     a[k+1]=b[k+1]=-1;
     int cur=0;
     for(i=0;i<n;i++)//n种物品;
     {
         for(j=min(v,cur+g[i].a);j>=g[i].a;j--)
         {
             for(t=1;t<=k;t++)
             {
                 a[t]=dp[j-g[i].a][t]||j==g[i].a&&t==1?dp[j-g[i].a][t]+g[i].b:0;
                 b[t]=dp[j][t];
             }
             x=y=z=1;
             while(z<=k&&(x<=k||y<=k))
             {
                 if(a[x]>b[y])
                 {
                     dp[j][z]=a[x];
                         x++;
                 }
                 else
                 {
                     dp[j][z]=b[y];
                     y++;
                 }
                // cout << dp[j][z] << endl;//不能去重;
                // if(dp[j][z]!=dp[j][z-1])
                z++;//此时dp[v][k]保存第k优解;
            }
         }
         cur=min(v,cur+g[i].a);
     }
     int res=0;
     for(int kk=1;kk<=k;kk++)
     {
         res+=dp[v][kk];
     }
     printf("%d\n",res);
}

int main()
{
     while(scanf("%d%d%d",&k,&v,&n)!=EOF)
     {
         for(int i=0;i<n;i++)
         scanf("%d%d",&g[i].a,&g[i].b);
         sort(g, g+n);
         solve();
     }
     return 0;
}


 

 
 

你可能感兴趣的:(再谈01背包--01背包第k优解)