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 nn monster crusade missions, such as black python and wild wolf. Completing the ii-th mission would consume Yuna h_ih i health points and s_is i stamina, and then she would get w_iw i gold coins. In the beginning, Yuna had HH health points and SS stamina. When her health points were dropped to less than or equal to 00, she would die. However, when her stamina was dropped to less than 00, 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 33, when her stamina dropped to -3−3, and then her stamina would be reset to 00. 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. Input The first line contains three integers n, H, Sn,H,S (1 \le n \le 1000, 1 \le H \le 300, 0 \le S \le 300)(1≤n≤1000,1≤H≤300,0≤S≤300). The next nn lines describe all the monster crusade missions, where the ii-th line contains three integers h_i, s_i, w_ih i ,s i ,w i (0 \le h_i, s_i \le 300, 1 \le w_i \le 10^9)(0≤h i ,s i ≤300,1≤w i ≤10 9 ). Output Print one integer – the maximum number of gold coins that Yuna could get. Sample 1 Inputcopy Outputcopy 2 66 22 1 23 2 66 8 90 2 Sample 2 Inputcopy Outputcopy 4 16 22 1 23 11 5 8 14 2 36 99 15 22 27 27
swjtu—春季集训 - Virtual Judge (vjudge.net)
思路:
- 01背包问题
- 有2个元素表示重量,分别枚举就行了
- 因为S有存在负数的转移, 就 把 s同意加上一个H,在dp的限制条件里在减掉一个H就行了。
#include <bits/stdc++.h> using namespace std; #define ri register int #define M 10005 template <class G> void read(G &x) { x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return ; } struct dian{ long long val,h,s; }p[1005]; long long dp[305][605]; int n,H,S; int main(){ read(n);read(H);read(S); for(ri i=1;i<=n;i++) read(p[i].h),read(p[i].s),read(p[i].val); for(ri i=1;i<=n;i++) { for(ri j=H;j-p[i].h>0;j--) { for(ri k=S+H;k-p[i].s+j-p[i].h-H>0;k--) { dp[j][k]=max(dp[j][k],dp[j-p[i].h][k-p[i].s]+p[i].val); } } } printf("%lld",dp[H][S+H]); }