poj 1013 great equipment(揹包,dp)

A - Great Equipment
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Once upon a time, there lived Catherine Ironfist, the Queen of Enroth. One day, she received the news of her father's death. So she sailed for Erathia to attend her father's funeral. Fearing the worst, she assembled a military fleet as her escort. On reaching the coast of Erathia, Catherine found an allied wizard's tower, devastated from battle and abandoned. There she learned that a black-hearted knight poisoned her father using a goblet of wine, and Erathia was falling to the enemies. And then, she mustered local armies, and marched to Erathia's castle, restoring lost land along the way.

During the battles, she found that the equipments for the soldiers were in urgent need. And she knew clearly that the best equipments were made by the battle dwarf's workshop in the world. The workshop's equipments were well known for the firmness and top-quality. ``Cloak of the Undead King", ``Armor of the Damned", ``Angelic Helm" are the nonesuch ones. But unfortunately, the workshop was seated at the Erathia's castle, the territory under the enemy's control. So she sent a brave volunteer to come into the castle and asked for the workshop's help.

``It's our pleasure to help the righteous heroine." Rion, the leader of the workshop sent the message to Catherine, `` We haven't enough resources to build the nonesuch equipments. So we'll try to offer the ordinary equipments as more as possible. Still, those ones are much better the equipments made by other workshops. But we have faced a difficult problem. The castle is in a state of siege. The guards prohibited the equipments to be carried away from the castle. We have to ask for the trade caravans' help. As you know, each trade caravan's capability of carrying equipments is limited. If they had carried a little more, they would be detected by the guards, which would lead them into the prison. So you have to arrange how to carry these equipments."

The workshop would offer helms, armors and boots. These three ones had different defend capabilities. Also, their weight and size were different from each other. What's more, Rion had told Catherine that if armed with those three ones together, they would form a set of equipments, which might provide much more defend capability. As far as the trade caravan was concerned, each one had its special weight limitation and size limitation. Catherine had to make the decision on how to arrange the transportation plan to provide her soldiers as more defend capabilities as possible. Could you help her to finish the plan?

Input 

The input describes several test cases. The first line of input for each test case contains a single integer n , the number of trade caravans(0$ \le$n$ \le$100) .

The following four lines describe the information of those equipments. The first line contains three integers w1 , s1 and d1 , indicating the weight, size and defend capabilities of the helm. The integers w2 , s2 and d2 in the second line represent the weight, size and defend capabilities of the armor. Also, in the third line, w3 , s3 and d3 are the weight, size and defend capabilities of the boot. The fourth line contains four integers c1 , c2 , c3 and d4 . Among those integers, c1 , c2 , c3 are the number of helms, armors and boots in a set of equipments, d4 is the capability of this set.

In the test case, following those data are n lines, describing the carrying capabilities of the trade caravans. Each line contains two integers,xi and yi , indicating the weight limit and size limit of a trade caravan.

The input is terminated by a descri_ption starting with n = 0. This descri_ption should not be processed.


Note: Because of the trade caravans' carrying capabilities, you may assume the quantities of the helms, armors and boots will not exceed 500 respectively.

Output 

Your program must compute the defend capability of the best carrying plan. That is, after having performed the carrying plan, the defend capability of the equipments which have been carried away from the castle should be the largest. For each test case in the input file, print the case number and a colon, and then the defend capability of those equipments.

Print a blank line between test cases.

Sample Input 

3
1 1 3
5 6 10
2 1 2
1 1 1 50
1 1
5 6
2 1
0

Sample Output 

Case 1: 50


設f[n][x][y] 表示前n輛車裝了x個裝備1和y個裝備2之後能裝的最多的裝備3的個數。

遞推關係如下:

f[n][x][y] = max ( f[n - 1][x - a][y - b] + c);    其中f[0][x][y]=0

 

其中a和b的範圍爲第n輛車可以裝的裝備1和裝備2的個數, c 爲第n輛車裝完a個裝備1和b個裝備2後可以裝的裝備3的個數。

通過遞推求得f[n][x][y]後,可以遍歷x和y(x,y的範圍可以在上述遞推的過程中得到)求得最終的結果。

由於f[n]只和f[n-1]有關,所以實際上 f 數組的第一維只需要2就可以(詳見代碼)

顯然,需要先得到可能是最優解的x,y,z的各種組合,再通過比較得到的最大總value。在相同x,y的情況下,我們總是儘量使z最大。直接用搜索必然很耗時間,這裏我們用DP來求x,y組合對應的z的最大值。狀態state[m][x][y]表示在使用1-m這m個揹包的情況下,取x,y值時,對應的z的最大值。即有狀態方程state[m][x][y]=max(state[m-1][x'][y']+zi),其中(xi+x'=x,yi+y'=y,xi*wx+yi*wy+zi*wz<=w[m],xi*sx+yi*sy+zi*sz<=s[m])(因爲要求zi要儘量大,所以同時會有xi*wx+yi*wy+(zi+1)*wz>w[m],xi*sx+yi*sy+(zi+1)*sz>s[m]).

最後ans=max(cal_value(x,y,state[n][x][y]))。






#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define rg(x,y) (x=x>y?x:y)
#define min(x,y) (x>y?y:x) 

int dp[2][501][501],w[501],s[501];
int main()
{
	int cs=0,n;
	int wx,wy,wz,sx,sy,sz,vx,vy,vz,c1,c2,c3,def;
	while(cin>>n&&n)
	{
		cin>>wx>>sx>>vx>>wy>>sy>>vy>>wz>>sz>>vz>>c1>>c2>>c3>>def;
		for(int i=1;i<=n;i++) cin>>w[i]>>s[i];
		memset(dp[0],0,sizeof(dp[0]));
		int ix,iy,iz,tempx,tempy,temp,maxx=0,maxy=0;
		for(int i=1;i<=n;i++)
		{
			memset(dp[1],0xff,sizeof(dp[1]));
			tempx=min(w[i]/wx,s[i]/sx); 
			for(ix=0;ix<=tempx;ix++)  
			{  
				tempy=min((w[i]-ix*wx)/wy,(s[i]-ix*sx)/sy);  
				for(iy=0;iy<=tempy;iy++)  
				{  
					iz=min((w[i]-ix*wx-iy*wy)/wz,(s[i]-ix*sx-iy*sy)/sz);  
					for(int a=0;a<=maxx;a++)  
					{  
						for(int b=0;b<=maxy;b++)  
						{  
							if(dp[0][a][b]>=0)  
							{  
								rg(dp[1][a+ix][b+iy],dp[0][a][b]+iz);  
							}  
						}  
					}  
				}  
				if(ix==0) temp=tempy;  
			}  
			memcpy(dp[0],dp[1],sizeof(dp[0]));  
			maxx+=tempx;  
			maxy+=temp;  
		}
		def-=c1*vx+c2*vy+c3*vz;  
		int ans=0;  
		for(int a=0;a<=maxx;a++)  
		{  
			for(int b=0;b<=maxy;b++)  
			{  
				if(dp[1][a][b]>=0)  
				{  
					rg(ans,a*vx+b*vy+dp[1][a][b]*vz+(def>0?def*min(a/c1,min(b/c2,dp[1][a][b]/c3)):0));  
				}  
			}  
		}  
		if(cs++) printf("\n");  
		printf("Case %d: %d\n",cs,ans);  
	}  
}



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