nyoj 49 Happy Xiaoming

happy xiaoming

Time Limit: 1000 ms | Memory Limit: 65535 KB

Difficulty: 4

describe

Xiao Ming is very happy today. The key to the new house he purchased at home is about to be handed over. There is a very spacious room for himself in the new house. What makes him even more happy is that his mother said to him yesterday: "What items need to be purchased in your room and how to arrange them, you have the final say, as long as it does not exceed N yuan." Xiaoming started making a budget this morning, but he wants to buy too many things, which will definitely exceed the N yuan limit set by his mother. Therefore, he specified an importance level for each item and divided it into 5 grades: represented by integers 1~5, and the 5th grade is the most important. He also looked up the price of each item (all in whole dollars) from the Internet. He wants to maximize the sum of the products of the price and importance of each item under the premise of not exceeding N dollars (which can be equal to N dollars). Suppose the price of the jth item is v[j], the importance is w[j], k items are selected in total, and the numbers are j1...jk, then the required sum is: v[j1]*w [j1]+..+v[jk]*w[jk] Please help Jin Ming to design a shopping list that meets the requirements.

enter

Input an integer N(0<N<=101) in the first line. It represents the number of test data groups
. The first line of each test data input is two positive integers, separated by a space:
N m
(where N (<30000 ) represents the total amount of money, m(<25) is the number of items you want to buy.)
From line 2 to line m+1, line j gives the
basic data of the item numbered j-1, each line There are 2 non-negative integers
vp
(where v represents the price of the item (v≤10000), p represents the importance of the item (1~5))

output

Each set of test data output has only one positive integer, which is the maximum value of the sum of the product of the price and the importance of the item that does not exceed the total amount of money
( <100000000)

sample input

1

1000 5

800 2

400 5

300 5

400 3

200 2

Sample output

3900

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
	int v,w;
}edge[10010];
bool cmp(node s1,node s2)
{
	return s1.v*s1.w>s2.v*s2.w;
}
int dp[150000];
intmain()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		memset(dp,0,sizeof(dp));
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++)
			scanf("%d %d",&edge[i].v,&edge[i].w);
		sort(edge,edge+m,cmp);
		for(int i=0;i<m;i++)
			for(int j=n;j>=edge[i].v;j--)
				dp[j]=max(dp[j],dp[j-edge[i].v]+edge[i].v*edge[i].w);
		printf("%d\n",dp[n]);
	}
	return 0;
}        

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324654271&siteId=291194637
49