动态规划训练5——Zoj3769
Diablo III
Time Limit: 2 Seconds Memory Limit: 65536 KB
Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new expansion of Diablo III, has been released! On hearing the news, the crazy video game nerd Yuzhi shouted: “I’m so excited! I’m so excited! I wanna kill the Diablo once more!”
The ROS introduced a lot of new features and changes. For example, there are two new attributes for players in the game: Damage and Toughness. The attribute Damage indicates the amount of damage per second you can deal and the Toughness is the total amount of raw damage you can take.
To beat the Diablo, Yuzhi need to select the most suitable equipments for himself. A player can carry at most 13 equipments in 13 slots: Head, Shoulder, Neck, Torso, Hand, Wrist, Waist, Legs, Feet, Shield, Weapon and 2 Fingers. By the way, there is a special type of equipment: Two-Handed. A Two-Handed equipment will occupy both Weapon and Shield slots.
Each equipment has different properties on Damage and Toughness, such as a glove labeled “30 20” means that it can increase 30 Damage and 20 Toughness for the player who equips it in the Hand slot. The total Damage and Toughness is the sum of Damage and Toughness of all equipments on the body. A player without any equipments has 0 Damage and 0 Toughness.
Yuzhi has N equipments stored in his stash. To fight against the Diablo without lose the battle, he must have at least M Toughness. In addition, he want to finish the battle as soon as possible. That means the Damage should be as much as possible. Please help Yuzhi to determine which equipments he should take.
Input
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains 2 integers N (1 <= N <= 300) and M (0 <= M <= 50000). The next N lines are the description of equipments. The i-th line contains a string Si and two integers Di and Ti (1 <= Di, Ti <= 50000). Si is the type of equipment in {“Head”, “Shoulder”, “Neck”, “Torso”, “Hand”, “Wrist”, “Waist”, “Legs”, “Feet”, “Finger”, “Shield”, “Weapon”, “Two-Handed”}. Di and Ti are the Damage and Toughness of this equipment.
Output
For each test case, output the maximum Damage that Yuzhi can get, or -1 if he can not reach the required Toughness.
Sample Input
2
1 25
Hand 30 20
5 25
Weapon 15 5
Shield 5 15
Two-Handed 25 5
Finger 5 10
Finger 5 10
Sample Output
-1
35
解题思路:
本题典型的分组背包问题,不过增加了一些限制条件。本题中有3个特殊物品:weapon、shield、two-handed和finger,不难想到可以将weapon和shield组合后加入two-handed组中,并将finger进行两两组合。
作为一个背包问题,首先我们能想到将防御值作为背包容量,并写出转移方程dp[i][j]=max(dp[i][j],dp[i-1][j-T]+D) (1)。然后对于处理本题中的难点:防御值不小于M。直接将M作为容量显然是会出现问题的,这时候就需要用到一个特殊技巧:转移方程等价于d[i][j+T]=max(dp[i][j+T],dp[i-1][j]+D) (2),通常在背包问题中我们常使用(1),因为它的递推关系更加明显,更适合写代码,然而这种形式过于强调了背包容量的上限,在本题这类题型中并不适用。
在本题中,我们可将防御值大于M的状态与等于M时合并,即dp[i][j]表示处理完了前i件装备,防御值为j时的最大攻击力,特别的当j=M时,表示防御值大于等于M的最大攻击力。在这种状态定义下,没有明确的背包容量,因此(1)并不适用,而(2)却依然能够使用。
所以代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cstdlib>
#include <string>
#include <string.h>
#include <map>
using namespace std;
const int maxn=50005,NINF=-1*(1<<30);
int dp[15][maxn],N,M,V;
struct Equip{
int D,T;
Equip(int x=0,int y=0):D(x),T(y){};
Equip operator+ (const Equip& x){
Equip t(D,T);
t.D+=x.D,t.T+=x.T;
return t;
}
};
vector<Equip> e[15],weapon,shield,finger;
map<string,int> ID;
bool cmp(vector<Equip> x,vector<Equip> y){
return x.size()>y.size();
}
bool build(void){
cin>>N>>M;
int mT=0,mD=0;
for(int i=0;i<11;++i) e[i].clear();
weapon.clear();shield.clear();finger.clear();
string str;Equip temp;
for(int i=0;i<N;++i){
cin>>str>>temp.D>>temp.T;
if(str=="Weapon") weapon.push_back(temp);
else if(str=="Shield") shield.push_back(temp);
else if(str=="Finger") finger.push_back(temp);
else e[ID[str]].push_back(temp);
}
if(weapon.size()==0)
for(int i=0;i<shield.size();++i)
e[9].push_back(shield[i]);
else if(shield.size()==0)
for(int i=0;i<weapon.size();++i)
e[9].push_back(weapon[i]);
else
for(int i=0;i<weapon.size();++i)
for(int j=0;j<shield.size();++j)
e[9].push_back(weapon[i]+shield[j]);
if(finger.size())
if(finger.size()==1) e[10].push_back(finger[0]);
else
for(int i=0;i<finger.size();++i)
for(int j=0;j<finger.size();++j)
if(i!=j)
e[10].push_back(finger[i]+finger[j]);
for(int i=0,tmax,d;i<11;++i){
tmax=0,d=0;
for(int j=0;j<e[i].size();++j)
if(e[i][j].T>tmax) tmax=e[i][j].T,d=e[i][j].D;
else if(e[i][j].T==tmax) d=max(e[i][j].D,d);
mT+=tmax,mD+=d;
}
if(mT<M){
cout<<-1<<endl;
return false;
}
else if(mT==M){
cout<<mD<<endl;
return false;
}
V=mT;return true;
}
int DP(void){
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
sort(e,e+11,cmp);
for(int i=1;i<=11;++i)
for(int j=0;j<=M;++j){
dp[i][j]=max(dp[i][j],dp[i-1][j]);
if(dp[i-1][j]!=-1)
for(int k=0,t;k<e[i-1].size();++k)
t=min(j+e[i-1][k].T,M),
dp[i][t]=max(dp[i][t],dp[i-1][j]+e[i-1][k].D);
}
return dp[11][M];
}
int main(void){
int T;cin>>T;
ID["Head"]=0,ID["Shoulder"]=1,ID["Neck"]=2,ID["Torso"]=3,
ID["Hand"]=4,ID["Wrist"]=5,ID["Waist"]=6,ID["Legs"]=7,
ID["Feet"]=8,ID["Two-Handed"]=9;
while(T--){
if(build()==false) continue;
cout<<DP()<<endl;
}
return 0;
}
本题卡了常数时间,因此处理背包前宜先按装备的数量给装备排序。