poj3624Charm Bracelet

简介:  Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23025   Accepted: 10358 Description Bess...

Charm Bracelet
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23025   Accepted: 10358

Description

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di

Output

* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

Sample Input

4 6
1 4
2 6
3 12
2 7

Sample Output

23
 
题意:01背包
重点:01背包、动态规划
难点:动态方程
 
#include<cstdio>
#include<cstring>
int main()
{
	int n,s,i,j,v[40000],w[40000],dp[40000];
	while(scanf("%d%d",&n,&s)==2)
	{
		memset(v,0,sizeof(v));
		memset(w,0,sizeof(w));
		memset(dp,0,sizeof(dp));

		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&w[i],&v[i]);
		}
		for(i=1;i<=n;i++)
			for(j=s;j>=w[i];j--)
			{
				if(dp[j]<dp[j-w[i]]+v[i])
					dp[j]=dp[j-w[i]] + v[i] ;
			} 
		printf("%d\n",dp[s]);
	}return 0;
}

目录
相关文章
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
PKU 3624 Charm Bracelet
本文主要讲背包入门题
|
人工智能 Go
ZOJ 3635 Cinema in Akiba
题意:一群人到电影院看电影,该电影的门票计算比较特殊,如:甲第一个拿到1号门票则位置为1,乙第二个拿票,票号也是1,则位置为2,因为1号位置已经被甲占了,乙的位置为剩下位置中的1号位置。
117 0
|
Java C语言
HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
139 0
[转]POJ3624 Charm Bracelet(典型01背包问题)
来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ Time Limit: 1000MS          Memory Limit: 65536K  ...
1234 0
|
存储 算法 测试技术