Original question: zoj 3769 http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3769.
A backpack with some limitations.
If there is no limit, the definition DP [I] [J] indicates the maximum damage value when the total toughness of the first class I item is J.
When the X item of the K class is obtained (the property value is D, T), there is a transfer equation: DP [k] [J + T] = max (DP [k] [J + T], DP [K-1] [J] + D ). when J + T exceeds m, you can calculate it by M.
However, the restrictions are as follows:
1. For two fingers, whether equipped with only one finger or two, they are represented by fingers, each finger is equipped with only one finger.
2. for weapon and shield and two-handed, we should treat them as one type. First, they must be regarded as one item, then we will enumerate the combination of weapon and shield into one type, which will be classified as one type.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <algorithm> # include <string> # include <vector> using namespace STD; # define n 50007 string God [15] = {"head", "shoulder", "neck", "Torso", "hand", "wrist", "waist ", "Legs", "feet", "finger", "shield", "Weapon", "two-handed"}; struct good {int damag, tough; good (INT _ damg, int _ togh) {damag = _ damg; tough = _ togh;} Good () {}}; vector <Good> G [14]; int getnum (string ka) {for (INT I = 0; I <13; I ++) {If (God [I] = ka) return I ;}} int DP [13] [N]; int main () {int I, j, k; string SS; int t, n, m; int damag, tough; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); for (I = 0; I <= 13; I ++) g [I]. clear (); for (I = 0; I <n; I ++) {CIN> SS; scanf ("% d", & damag, & tough ); k = getnum (SS); G [K]. push_back (good (damag, tough); If (k = 10 | K = 11) // weapon or Sheild, which is included in two_handed G [12]. push_back (good (damag, tough);} // enumerate weapon and Sheild's combination for (I = 0; I <G [10]. size (); I ++) for (j = 0; j <G [11]. size (); j ++) g [12]. push_back (good (G [10] [I]. damag + G [11] [J]. damag, G [10] [I]. tough + G [11] [J]. tough); G [10]. clear (); G [11]. clear (); // G [10] All finger cases (separate and combined) for (I = 0; I <G [9]. size (); I ++) {G [10]. push_back (G [9] [I]); For (j = I + 1; j <G [9]. size (); j ++) g [10]. push_back (good (G [9] [I]. damag + G [9] [J]. damag, G [9] [I]. tough + G [9] [J]. tough);} G [9]. clear (); // note that the values are cleared and added to G [10] To memset (DP,-1, sizeof (DP )); DP [11] [0] = 0; int T, D; for (I = 0; I <G [12]. size (); I ++) {good G = G [12] [I]; t = min (G. tough, m); DP [11] [T] = max (DP [11] [T], G. damag) ;}for (k = 10; k> = 0; k --) {for (I = 0; I <= m; I ++) {DP [k] [I] = max (DP [k] [I], DP [k + 1] [I]); if (DP [k + 1] [I] =-1) continue; For (j = 0; j <G [K]. size (); j ++) {good G = G [k] [J]; t = min (G. tough + I, m); D = G. damag + dp [k + 1] [I]; DP [k] [T] = max (DP [k] [T], d );}}} printf ("% d \ n", DP [0] [m]);} return 0 ;}View code