ZOJ - 3769 - Diablo III (分组背包 ,dp)

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
Author: YU, Zhi; JIANG, Kai
Source: The 14th Zhejiang University Programming Contest

题目链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769


题意

  • 多组输入
  • 每组先输入 nm
  • n : 总共有 n 件装备。 m : 挑选的装备耐久度之和要大于等于 m
  • 接下来的 n 行,第 i 行,输入第 i 件装备的种类 Si ,伤害值 Di ,耐久度 Ti
  • 条件1:如果装备了 Two-Handed , 就不能装备 WeaponShield 其中任一一种装备。
  • 条件2:可以选择装备一个 Finger ,也可以选择装备两个 Finger,其他相同种类的装备只能装备一个。

:在满足所挑选的装备的耐久度之和大于等于 m 条件下,使得所挑选装备的总伤害值最大。若存在,输出总伤害值,否则,输出 -1


解题思路

  • 若不考虑 条件1条件2 的话,这道题就相当于是 分组背包
  • 考虑 条件1 :既然装备了 Two-Handed , 就不能装备 WeaponShield 其中任一一种装备的话,那我们就可以把【单个 Two-Handed】,【单个 Weapon】,【单个 Shield】和【WeaponShield 两两组合】当做同一种种类的装备,这样就不会冲突了。
  • 考虑 条件2 :我们可以把【单个 Finger 】和【FingerFinger 两两组合】(要排除自己和自己组合)当做同一种种类的装备。
  • 状态

    dp[i][j] d p [ i ] [ j ] : 后 i 种物品,耐久度达到 j 时 ,所能得到的最大伤害值。(因为题目数据的原因,所以要倒着来)

  • 转移方程
    不选第 i 种装备:
    dp[i][j]=max(dp[i][j],dp[i+1][j]); d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i + 1 ] [ j ] ) ;

    选第 i 种装备:
    dp[i][lim]=max(dp[i][lim],dp[i+1][j]+cnt.d); d p [ i ] [ l i m ] = m a x ( d p [ i ] [ l i m ] , d p [ i + 1 ] [ j ] + c n t . d ) ;
    (lim=min(m,lim+cnt.t)) ( l i m = m i n ( m , l i m + c n t . t ) )

    第二维不一定要写 j , 这样就不用考虑耐久度超过 m 了。

  • 最后答案: dp[1][m] d p [ 1 ] [ m ]

代码如下:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
using namespace std;

map<string,int> f;
int dp[15][50005];//dp[i][j] : 后 i 种物品,耐久度达到 j 时 ,所能得到的最大伤害值。

struct X{
    int d,t;
};

vector<X> p[20];

void Init(){
    f["Head"] = 1 , f["Shoulder"] = 2 , f["Neck"] = 3;
    f["Torso"] = 4 , f["Hand"] = 5 , f["Wrist"] = 6;
    f["Waist"] = 7 , f["Legs"] = 8 , f["Feet"] = 9;
    f["Finger"] = 12 , f["Shield"] = 13 , f["Weapon"] = 14;
    f["Two-Handed"] = 15;
}

void slove1(){//处理 Finger。合并为编号 10 种类的装备。
    for(int i = 0;i < p[12].size();i++){
        p[10].push_back(p[12][i]);
        for(int j = i + 1;j < p[12].size();j++){
            X cnt1,cnt2;
            cnt1 = p[12][i];
            cnt2 = p[12][j];
            cnt1.d += cnt2.d;
            cnt1.t += cnt2.t;
            p[10].push_back(cnt1);
        }
    }
}

void slove2(){//处理 Shield ,  Weapon , Two-Handed。合并为编号 11 种类的装备。
    X cnt1,cnt2;
    for(int i = 0;i < p[14].size();i++) p[11].push_back(p[14][i]);
    for(int i = 0;i < p[13].size();i++){
        p[11].push_back(p[13][i]);
        for(int j = 0;j < p[14].size();j++){
            cnt1 = p[13][i];
            cnt2 = p[14][j];
            cnt1.d += cnt2.d;
            cnt1.t += cnt2.t;
            p[11].push_back(cnt1);
        }
    }
    for(int i = 0;i < p[15].size();i++){
        p[11].push_back(p[15][i]);
    }
}

int main(){
    int t;
    Init();
    while(cin >> t){
        while(t--){
            for(int i = 1;i <= 20;i++) p[i].clear();
            memset(dp,-1,sizeof(dp));
            int n,m;
            cin >> n >> m;
            for(int i = 1;i <= n;i++){
                X cnt;
                int x,d,t;
                string s;
                cin >> s >> d >> t;
                x = f[s];
                cnt.d = d,cnt.t = t;
                p[x].push_back(cnt);
            }
            slove1();
            slove2();
            dp[12][0] = 0;
            for(int i = 11;i >= 1;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) continue;//这里表示 dp[i + 1][j] 状态不存在(在这道题等价于初始化为负无穷)。
                    for(int k = 0;k < p[i].size();k++){// dp 的含义是当前状态的最优解,不管当前状态之前怎么选。
                        int lim;
                        X cnt;
                        cnt = p[i][k];
                        if(j + cnt.t > m) lim = m;//第二维不一定要写 j , 这样就不用考虑耐久度超过 m 了。
                        else lim = j + cnt.t;
                        dp[i][lim] = max(dp[i][lim],dp[i + 1][j] + cnt.d);
                    }
                }
            }
            cout << dp[1][m] << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值