第十一届山东省大学生程序设计竞赛H-Adventurer‘s Guild(二维费用的背包问题)

H-Adventurer’s Guild

题目描述:

Yuna traveled into the fantasy world. The gods of this world gave her a powerful set of equipment so that she could defeat many fierce monsters. However, she had limited health points and stamina, and she could not kill a large number of monsters.

Adventurer’s guild would release n monster crusade missions, such as black python and wild wolf. Completing the i-th mission would consume Yuna hi health points and si stamina, and then she would get wi gold coins.

In the beginning, Yuna had H health points and S stamina. When her health points were dropped to less than or equal to 0, she would die. However, when her stamina was dropped to less than 0, she would not give up, and then the overdrawn stamina would be reduced from health points. For example, her health points would be reduced by 3, when her stamina dropped to −3, and then her stamina would be reset to 0. If her health points can not afford the overdrawn stamina, she would also die.

As a friend of Yuna, can you help her choose some missions to complete to get the maximum number of gold coins? Make sure Yuna does not die, or you will be very sad.

输入描述:

The first line contains three integers n, H, S (1 ≤ n ≤ 1000, 1 ≤ H ≤ 300, 0 ≤ S ≤ 300).

The next n lines describe all the monster crusade missions, where the i-th line contains three integers hi, si, wi (0 ≤ hi, si ≤ 300, 1 ≤ wi ≤ 10^9).

输出描述:

Print one integer – the maximum number of gold coins that Yuna could get.

示例1

输入

2 66 22
1 23 2
66 8 90

输出

2

示例2

输入

4 16 22
1 23 11
5 8 14
2 36 99
15 22 27

输出

27

题目大意:

有h点生命和s点耐力,打每个怪物都要消耗生命和耐力并获得一定的金币,生命值可以转化为耐力值,当生命为0时玩家就会死亡。

给定每个怪物要消耗的生命和耐力和获得的金币,问在不死的情况下最多可以获得多少个金币。

题解:

二维费用的背包问题模板题,只要增加当体力值不够,生命值转化为体力值时的状态转移方程即可。

二维费用的背包问题:

给定背包能承受的最大体积和最大重量。给定一些物体,求解将哪些物品装入背包,可使物品总体积不超过背包容量,总重量不超过背包可承受的最大重量,且价值总和最大。

相比01背包,每个物品多了重量这一个属性。

状态转移方程

在这里插入图片描述

状态压缩成二维

在这里插入图片描述

二维费用的背包问题模板:

#include <iostream>
using namespace std;
const int N = 1e3 + 5;
int n, V, M, f[N][N];
int main() {
    cin >> n >> V >> M;
    for (int i = 0; i < n; i ++) {
        int v, m, w;
        cin >> v >> m >> w;
        for (int j = V; j >= v; j --)
            for (int k = M; k >= m; k --)
                f[j][k] = max(f[j][k], f[j - v][k - m] + w);
    }
    cout << f[V][M] << endl;
    return 0;
}

题解代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3+5;
ll n, H, S, f[N][N];
int main() {
    cin >> n >> H >> S;
    H--; // 生命值不能为0, 保留1 
    for (ll i = 0; i < n; i ++) {
        ll h, s, w;
        cin >> h >> s >> w;
        for (ll j = H; j >= h; j --)
            for (ll k = S; k >= 0; k --){
                if(k-s<0 && j-h-(s-k)>=0) f[j][k] = max(f[j][k],f[j-h-(s-k)][0] + w); // 生命和耐力都够 
                else if(j-h>=0 && k-s>=0) f[j][k] = max(f[j][k],f[j-h][k-s] + w); // 生命值换耐力值且生命不会为0 
            }
    }
    cout << f[H][S] << '\n'; // 输出最大金币数量 
    return 0;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值