Problem Description
In an online game, “Lead of Wisdom” is a place where the lucky player can randomly get powerful items.
There are k types of items, a player can wear at most one item for each type. For the i-th item, it has four attributes ai,bi,ci and di. Assume the set of items that the player wearing is S, the damage rate of the player DMG can be calculated by the formula:
DMG=(100+∑i∈Sai)(100+∑i∈Sbi)(100+∑i∈Sci)(100+∑i∈Sdi)
Little Q has got n items from “Lead of Wisdom”, please write a program to help him select which items to wear such that the value of DMG is maximized.
Input
The first line of the input contains a single integer T (1≤T≤10), the number of test cases.
For each case, the first line of the input contains two integers n and k (1≤n,k≤50), denoting the number of items and the number of item types.
Each of the following n lines contains five integers ti,ai,bi,ci and di (1≤ti≤k, 0≤ai,bi,ci,di≤100), denoting an item of type ti whose attributes are ai,bi,ci and di.
Output
For each test case, output a single line containing an integer, the maximum value of DMG.
Sample Input
1
6 4
1 17 25 10 0
2 0 0 25 14
4 17 0 21 0
1 5 22 0 10
2 0 16 20 0
4 37 0 0 0
Sample Output
297882000
题意:有n个武器,都属于k种武器种其中一种。从n个武器种选出一个集合S,S种每种武器最多只能出现一次。每种武器有a,b,c,d四个值,均大于零。求DMG最大的S。
DMG=(100+∑i∈Sai)(100+∑i∈Sbi)(100+∑i∈Sci)(100+∑i∈Sdi)
思路:最开始以为又是动态规划,看到都头疼,后面看数据量和时间,用暴力dfs,居然过了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 60;
int cnt[N];
int next_[N];
int temp[N][N][4];
void dfs(int x, ll a, ll b, ll c, ll d, int m, ll &ans)
{
if (x > m)
{
ll tmp = a * b*c*d;
if (tmp > ans)
{
ans = tmp;
}
return;
}
int num = cnt[x];
if (!num)
{
dfs(next_[x], a, b, c, d, m, ans);
return;
}
for (int i = 1; i <= num; i++)
{
dfs(x + 1, a + temp[x][i][0], b + temp[x][i][1], c + temp[x][i][2], d + temp[x][i][3], m, ans);
}
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n;
int k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= k; i++)
{
cnt[i] = 0;
}
int x;
while (n--)
{
scanf("%d", &x);
cnt[x]++;
for (int j = 0; j < 4; j++)
{
scanf("%d", &temp[x][cnt[x]][j]);
}
}
x = k + 1;
for (int i = k; i; i--)
{
next_[i] = x;
if (cnt[i])
{
x = i;
}
}
ll ans = 0;
dfs(1, 100, 100, 100, 100, k, ans);
printf("%lld\n", ans);
}
}