HDU 2639——Bone Collector II【01揹包、第k優解】

題目傳送門

求價值第K大的01揹包問題,技巧是多加一維表示第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 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件物品的價值和體積,問在總體積不超過v時的第k大價值


分析:

第K優解問題

其基本思想是將每個狀態都表示成有序隊列,將狀態轉移方程中的max/min轉化成有序隊列的合併。這裏仍然以01揹包爲例講解一下。
首先看01揹包求最優解的狀態轉移方程:f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}。如果要求第K優解,那麼 狀態f[i][v]就應該是一個大小爲K的數組f[i][v][1…K]。其中f[i][v][k]表示前i個物品、揹包大小爲v時,第k優解的值。 “f[i][v] 是一個大小爲K的數組”這一句,熟悉C語言的同學可能比較好理解,或者也可以簡單地理解爲在原來的方程中加了一維。顯然 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]。總的複雜度是O(VNK)。

01揹包再清楚不過了,主要還是是有序隊列歸併的問題。


AC代碼:

#include <iostream>
#include <vector>
#include <utility>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <fstream>
#include <set>
using namespace std;
typedef long long ll;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int MAXN = 1e3+5;
#define INF 0x3f3f3f

int dp[MAXN][31];
int w[MAXN];
int v[MAXN];
int a[31];
int b[31];
int main() {
	int T;
	while (cin >> T) {
		while (T--) {
			memset(dp, 0, sizeof(dp));
			int n, m, k;
			cin >> n >> m >> k;
			for (int i = 1; i <= n; i++) {
				cin >> v[i];
			}
			for (int i = 1; i <= n; i++) {
				cin >> w[i];
			}

			for (int i = 1; i <= n; i++) {
				for (int j = m; j >= w[i]; j--) {
					int cnt = 0, d;
					for (d = 1; d <= k; d++) {
						a[d] = dp[j - w[i]][d] + v[i];
						b[d] = dp[j][d];
					}
					// 二路歸併
					int x, y, z;
					x = y = z = 1;
					a[d] = b[d] = -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++;
						}
						if (dp[j][z] != dp[j][z - 1])
							z++;
					}
				}
			}
			cout << dp[m][k] << endl;
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章