PACKING 012背包.。。

问题 K: PACKING

时间限制: 1 Sec  内存限制: 128 MB
提交: 351  解决: 46
[提交] [状态] [命题人:admin]

题目描述

It was bound to happen.  Modernisation has reached the North Pole.  Faced with escalating costs for feeding Santa Claus and the reindeer, and serious difficulties with security, NP Management has decided to do away with the traditional sleigh and adopt delivery by drone (magic, superfast drone).  
Lack of investment capital means that the new system will start small, and hopefully grow in the years to come.  For the first test run in 2017 there will be only two drones and they will have limited carrying capacity.  PR is, of course, all important.  There will be disappointment, and NP Management has decided to focus on delivering only the most expensive toys to the richest children, so as to focus the worst of the disappointment on those who have the greatest experience of coping (the poor). 
Choosing the presents to deliver is your problem.  You are being asked to develop an algorithm to select the cargo to deliver, given weight limits for each of the drones and a list of candidate presents with weights and values.  Your goal is to maximise the value of gifts delivered. 

 

输入

Input will consist of a series of problems.  The first line of the input holds a single integer P being the number of problems.  Then for each problem there will be three lines of input.  The first line holds three integers:  N (1 <= N <= 100) being the number of candidate presents;  W1 and W2 (1 <= W1, W2 <= 1000) being the weight limits of the two drones respectively.  The second line holds N integers (1 <= wi  <= 100) being the weights of each of the candidate presents and the third line holds N integers (1 <= vi  <= 100) being the values of the presents (in thousand dollar units).  All lines are formatted with single spaces between numbers and no leading or trailing spaces. 

 

输出

For each problem your program should output one line with the text “Problem “ and the number of the problem (counting from 1) followed by a colon, a space and the total value of presents shipped by the drone pair. 

 

样例输入

复制样例数据

2
4 9 4
3 4 5 6
5 7 9 10
6 9 11
3 4 5 6 3 4
2 3 4 5 3 3

样例输出

Problem 1: 22
Problem 2: 16

题意:两个背包问能装的最大价值

其实有了01背包的基础应该可以推出这个题的状态转移方程的。就是二维转成了三维呗。一开始一直在用dfs剪枝做然后各种优化还是没用。。。

先贴一个01包的代码

 

#include
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
using namespace std;
const ll mod=1e9+7;
const ll maxx=70+10;
ll w[maxx];
ll v[maxx];
ll dp[3000][3000];
int main()
{
	int i,j,k,n,m,t,sum; 

    while(scanf("%d",&sum)!=EOF)
    {
	    if(sum==0) 
	    {
			return 0;
		}
	    scanf("%d",&n);
	    for(i=1;i<=n;i++)
	    {
	    	scanf("%d%d",&w[i],&v[i]);
		}
		mem(dp,0);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=sum;j++)
			{
				if(j>=w[i])
				{
					dp[i][j]=max((dp[i-1][j-w[i]]+v[i]),dp[i-1][j]);	
				}	
				else
					dp[i][j]=dp[i-1][j];
			}
		}
		printf("%d\n",dp[n][sum]);
	}
    
    return 0;
}

 

然后是这个012背包的代码,不过需要用到O2优化不然会T,而且不用三目运算符也会T,可是有意思了这个题。设dp[i][j]代表W1=i W2=j(两个背包的容量)是能装的最大价值。

#include
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#pragma GCC optimize(2)
using namespace std;
const ll mod=1e9+7;
const ll maxx=1e3+10;
int w[maxx];
int v[maxx];
int dp[maxx][maxx];
int main()
{
	int i,j,k,n,m,t,sum,p;
	int W1,W2; 
	scanf("%d",&t);
	for(p=1;p<=t;p++)
    {
    	//for(i=0;i<=W1;i++)
        //for(j=0;j<=W2;j++) dp[i][j]=0;
        mem(dp,0);
    	scanf("%d%d%d",&n,&W1,&W2);
    	for(i=1;i<=n;i++)
    	scanf("%d",&w[i]);
    	for(i=1;i<=n;i++)
    	scanf("%d",&v[i]);
	    for(i=1;i<=n;i++)
	    {
	    	for(j=W1;j>=0;j--)
	    	{
	    		for(k=W2;k>=0;k--)
	    		{
	    			if(j>=w[i]) 
	    			{
	    				dp[j][k]=dp[j][k]>dp[j-w[i]][k]+v[i] ? dp[j][k]:dp[j-w[i]][k]+v[i];
					}
					if(k>=w[i])
					{
						dp[j][k]=dp[j][k]>dp[j][k-w[i]]+v[i] ? dp[j][k]:dp[j][k-w[i]]+v[i];
					}
				}
			}
		}
		printf("Problem %d: %d\n", p, dp[W1][W2]);
	}
    
    return 0;
}

 

你可能感兴趣的:(PACKING 012背包.。。)