1. 程式人生 > 實用技巧 >HDU6672 Lead of Wisdom(爆搜)

HDU6672 Lead of Wisdom(爆搜)

Lead of Wisdom

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 \(a_i,b_i,c_i\)

and \(d_i\). 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+\Sigma a_i)(100+\Sigma b_i)(100+\Sigma c_i)(100+\Sigma d_i)\)

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 \(t_i*,*a_i,b_i,c_i, d_i\)

$ (1≤t_i≤k,0≤a_i,b_i,c_i,d_i≤100)$, denoting an item of type \(t_i\)

whose attributes are $a_i,b_i,c_i $and \(d_i\)

.

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

暴力簽到,對於這個資料規模直接爆搜即可,複雜度證明。可以用陣列模擬一下連結串列,跳過沒用的組。

當時沒做出來,反思一下:當時寫A有點懷疑人生,加上隊友告訴我是個DP,加上判斷題目型別的能力很弱....其實還是自己辣雞T^T

這題本地跑了14s一度以為哪裡出問題了,沒想到交上去過了2333

#include <bits/stdc++.h>
using namespace std;
struct item
{
	long long a, b, c, d;
};
vector<vector<item> > v;
int n, k;
long long ans = -1e18;
int Next[55];
void dfs(int x, long long a, long long b, long long c, long long d)
{
	if(x == -1)
	{
		long long sum = a * b * c * d;
		ans = max(ans, sum);
		return;
	}
	for(int i = 0; i < v[x].size(); i++)
	{
		dfs(Next[x], a + v[x][i].a, b + v[x][i].b, c + v[x][i].c, d + v[x][i].d);
	}
}
int main()
{
	//freopen("1010.in","r",stdin);
	//freopen("myout.out","w",stdout);
	int Case;
	cin >> Case;
	for(int i = 1; i <= 51; i++)
	{
		vector<item> temp;
		v.push_back(temp);
	}
	while(Case--)
	{
		cin >> n >> k;
		ans = -1e18;
		set<int>s;
		//memset(Next, 0, sizeof(Next));
		long long sa = 100, sb = 100, sc = 100, sd = 100;
		for(int i = 1; i <= 51; i++)
		{
			v[i].clear();
		}
		for(int i = 1; i <= n; i++)
		{
			int t, a, b, c, d;
			scanf("%d%d%d%d%d", &t, &a, &b, &c, &d);
			v[t].push_back(item{a, b, c, d});
			s.insert(t);
		}
		set<int>::iterator it;
		int start, before;
		bool began = 0;
		for(it = s.begin(); it != s.end(); it++)
		{
			if(v[*it].size() == 1)
			{
				sa += v[*it][0].a;
				sb += v[*it][0].b;
				sc += v[*it][0].c;
				sd += v[*it][0].d;
			}
			else if(!began) 
			{
				start = before = *it;
				began = 1;
			}
			else 
			{
				Next[before] = *it;
				before = *it;
			}
		}
		Next[before] = -1;
		dfs(start, sa, sb, sc, sd);
		cout << ans << endl;
	}
	return 0;
}