Schrödinger's Knapsack(dp)

Schrödinger's Knapsack

Time Limit: 1 Second      Memory Limit: 65536 KB

DreamGrid has a magical knapsack with a size capacity of  called the Schrödinger's knapsack (or S-knapsack for short) and two types of magical items called the Schrödinger's items (or S-items for short). There are  S-items of the first type in total, and they all have a value factor of ; While there are  S-items of the second type in total, and they all have a value factor of . The size of an S-item is given and is certain. For the -th S-item of the first type, we denote its size by ; For the -th S-item of the second type, we denote its size by .

But the value of an S-item remains uncertain until it is put into the S-knapsack (just like Schrödinger's cat whose state is uncertain until one opens the box). Its value is calculated by two factors: its value factor , and the remaining size capacity  of the S-knapsack just after it is put into the S-knapsack. Knowing these two factors, the value  of an S-item can be calculated by the formula .

For a normal knapsack problem, the order to put items into the knapsack does not matter, but this is not true for our Schrödinger's knapsack problem. Consider an S-knapsack with a size capacity of 5, an S-item with a value factor of 1 and a size of 2, and another S-item with a value factor of 2 and a size of 1. If we put the first S-item into the S-knapsack first and then put the second S-item, the total value of the S-items in the S-knapsack is ; But if we put the second S-item into the S-knapsack first, the total value will be changed to . The order does matter in this case!

Given the size of DreamGrid's S-knapsack, the value factor of two types of S-items and the size of each S-item, please help DreamGrid determine a proper subset of S-items and a proper order to put these S-items into the S-knapsack, so that the total value of the S-items in the S-knapsack is maximized.

Input

The first line of the input contains an integer  (about 500), indicating the number of test cases. For each test case:

The first line contains three integers  and  (), indicating the value factor of the first type of S-items, the value factor of the second type of S-items, and the size capacity of the S-knapsack.

The second line contains two integers  and  (), indicating the number of the first type of S-items, and the number of the second type of S-items.

The next line contains  integers  (), indicating the size of the S-items of the first type.

The next line contains  integers  (), indicating the size of the S-items of the second type.

It's guaranteed that there are at most 10 test cases with their  larger than 100.

Output

For each test case output one line containing one integer, indicating the maximum possible total value of the S-items in the S-knapsack.

Sample Input

3
3 2 7
2 3
4 3
1 3 2
1 2 10
3 4
2 1 2
3 2 3 1
1 2 5
1 1
2
1

Sample Output

23
45
10

Hint

For the first sample test case, you can first choose the 1st S-item of the second type, then choose the 3rd S-item of the second type, and finally choose the 2nd S-item of the first type. The total value is .

For the second sample test case, you can first choose the 4th S-item of the second type, then choose the 2nd S-item of the first type, then choose the 2nd S-item of the second type, then choose the 1st S-item of the second type, and finally choose the 1st S-item of the first type. The total value is .

The third sample test case is explained in the description.

It's easy to prove that no larger total value can be achieved for the sample test cases.

思路 :

一開始沒擺脫揹包的束縛,還是不能深刻理解揹包吧,老在糾結體積,一看體積那麼大,就應該反應過來,並非傳統的揹包問題,其實每個題目,尤其是dp題目,萬變中總有不變的地方,那就是總會有數組恰好能容下的狀態表示方法。故而看到兩類物品個數2000,,此處應該是關鍵所在。將問題簡化一下,若只有一類物品,則容易想到,體積小的物品必在前面。那麼實際上問題就轉化爲兩隊物品,排好隊,各取前幾個的問題。因而可以想到狀態的表示方法:dp[i][j] 表示1隊取i個物品,2隊取j個物品的最優答案。顯然狀態轉移方程dp[i][j] = max(dp[i-1][j] + v1 , dp[i][j-1] + v2);

小結:dp的題目應該從題目中給的數據量較小的變量入手,找到狀態表示方法,進而找到轉移方程。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;

const int maxn = 2e3+5;

ll a[maxn],b[maxn];
ll suma[maxn],sumb[maxn];
ll dp[maxn][maxn];

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		 ll k1,k2,c;
		 int m,n;
		 scanf("%lld%lld%lld",&k1,&k2,&c);
		 scanf("%d%d",&m,&n);
		 for(int i = 1; i <= m ; i++)
		 {
		     scanf("%lld",&a[i]);
		 }
		 for(int j = 1; j <= n ; j++)
		 {
			 scanf("%lld",&b[j]);
		 }
		 sort(a+1,a+m+1);
		 sort(b+1,b+n+1);
		 suma[0] = sumb[0] = 0;
		 for(int i = 1; i <= m ; i++)
		 {
			 suma[i] =a[i] + suma[i-1];
		 }
		 for(int j = 1; j <= n ; j++)
		 {
			 sumb[j] =b[j] + sumb[j-1];
		 }
		 dp[0][0] = 0;
		 dp[0][1] = k2*(c-b[1]);
		 dp[1][0] = k1*(c-a[1]);
		 ll ans = max(dp[0][1],dp[1][0]);
		 for(int i = 0; i <= m; i++)
		 for(int j = 0; j <= n; j++)
		 {
			 if(i == 0 && j == 0) continue;
			 ll v1 = i > 0 ? dp[i-1][j] + (c-suma[i]-sumb[j])*k1 : 0;
			 ll v2 = j > 0 ? dp[i][j-1] + (c-suma[i]-sumb[j])*k2 : 0;
			 dp[i][j] = max(v1,v2);
			 ans = max(ans,dp[i][j]);
		 }
		 if(ans < 0) ans = 0;
		 cout << ans <<endl;
	}
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章