[] Knapsack problem dynamic programming - examples analysis

Variant multiple knapsack problem

Equipment to strengthen

Title Description
online games, is a common method of lifting equipment to strengthen the role of combat capability. Now you involved in the development of the game also has this feature, the team is designing each piece of equipment to strengthen the combat capability and the number of coins that can enhance the need to consume. In order to design a reasonable system strengthening, we decided to do some strengthening simulation tests, and you are now in the simulation program developers. Suppose there are n pieces of equipment that can be worn at the same time, for the i pieces of equipment, can strengthen mi times at most, for the i-th element of the j-th strengthen the equipment, will increase the combat power of fij and gij need to consume coins. Now data are given all the equipment, and the initial number of coins owned by seeking the maximum number of combat power can be increased.
Input Description:
The first line of the input file a positive integer T, represents a set of test data.
Then there are T set of data. Each set of data of the first two acts of integers n and s, respectively, represents the number of equipment and the number of coins have initially. Followed by n lines, each line represents an equipment, wherein each of the first row is a string representing the name of the equipment, mi The second non-negative integer representing the number of equipment may strengthen the most, followed by 2 * mi a non-negative integer representing each will strengthen the combat power and increase the number of gold consumption.

Data range:
for all data files, 1 <= T <= 10 . The maximum length of the name of the equipment is not more than 32 characters, the lowercase letters or numbers. Each piece of equipment most often reinforced mi satisfies 0 <= mi <= 3. All combat power values and the number of gold coins and not more than 10,000,000 non-negative integer.
For 20% of the data therein, a number satisfying equipment 1 <= n <= 5;
another 30% of the data, a number satisfying equipment 1 <= n <= 10;
another 30% of the data, a number satisfying equipment 1 <= n <= 16;
last remaining 20% of the data, the equipment number satisfying 1 <= n <= 20.

Data intensive program to ensure that there is only able to achieve maximum combat power.

Description Output:
For each test data, the behavior of a first integer, represents the most liftable combat capability. Followed by n lines, each representing each piece of equipment to strengthen the number of times the combat power to achieve this, in the form of "equipment to strengthen the number + name." Sequentially outputting the input equipment to be consistent with the data. In more detail, refer to the format of the sample output.

Input examples:
. 3
. 4 100
Helm,. 1 20 is 30
Gloves. 1 30 40
Boots. 1 10 10
Sword. 1 50 50
. 5 10
Item1 0
Item2. 1 10000 100
Item3. 1 10 10
Item4. 3. 1 0 2. 1. 3 2
ITEM5. 3 0. 4. 1. 1. 5. 1
256. 6
Ix 12 is 43 is 35 2 58
2x 88 31 is 54 is 34 is 2
3x 2 10 15 55 43 is
4x. 19. 19 2 32 54 is
5X. 1 15 45 2 84
6X 99 12 is 2 40. 19

Output Examples:
90
Helm, + 0
Gloves +. 1
Boots +. 1
Sword +. 1
12 is
Item1 + 0
Item2 + 0
Item3 + 0
Item4 +. 3
ITEM5 +. 3
418
Ix + 0
2x + 2
3x +. 1
4x + 2
5X +. 1
6X + 2

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

//dp方程
const int max_n = 20 + 5;
const int max_s = 100 + 10;
struct Equipment {
	string name;
	int mi;
	vector<int> fij; //战力
	vector<int> gij; //金币
};

int main()
{
	int T;
	cin >> T;
	for (int c = 0; c < T; c++)
	{
		int n, s;
		cin >> n >> s;

		vector<Equipment> items(n+1); //n个装备

                //注意这里用的是i=1,因为我们希望i就代表第i个装备,0用来初始化dp中的0位置

		for (int i = 1; i <= n; i++)
		{
			cin >> items[i].name >> items[i].mi;
			for (int j = 0; j < items[i].mi; j++)
			{
				int fij, gij;
				cin >> fij >> gij;
				items[i].fij.push_back(fij);
				items[i].gij.push_back(gij);
			}
		}

		//输入完成

		int dp[max_n][max_s] = { 0 };
		int result[max_n][3] = { 0 };
                  
		for (int i = 1; i <= n; i++) //每个装备,注意这里也是i=1,i<=n
		{
			for (int j = s; j > 0; j--)
			{
				for (int k = 0; k < items[i].mi; k++)
				{
                                        int k_f = 0;//升级K次得到的战力
					int k_g = 0; //升级K次用掉的金币					

					for (int n = 0; n <= k; n++)
					{
						k_f += items[i].fij[n];
						k_g += items[i].gij[n];
					}

					if (j - k_g >= 0)
					{
                                               dp[i][j] = max(dp[i][j], dp[i - 1][j - k_g] + k_f);
					}
				}
			}
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j < s; j++)
			{
				cout << dp[i][j]<<"  ";
			}
			cout << dp[i][s]<<endl;
		}
	}
}

In this dp inside, there is a problem to be solved is how to upgrade the output of those (select those items, the problem suspend)
----------------- --- Construction ------------------

Guess you like

Origin www.cnblogs.com/EvansPudding/p/12615140.html