hdu 2660 Accepted Necklace(01-背包变形 || DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2660

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

Accepted Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2557    Accepted Submission(s): 1017

Problem Description

I have N precious stones, and plan to use K of them to make a necklace for my mother, but she won‘t accept a necklace which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valuable
necklace my mother will accept.

Input

The first line of input is the number of cases.

For each case, the first line contains two integers N (N <= 20), the total number of stones, and K (K <= N), the exact number of stones to make a necklace.

Then N lines follow, each containing two integers: a (a<=1000), representing the value of each precious stone, and b (b<=1000), its weight.

The last line of each case contains an integer W, the maximum weight my mother will accept, W <= 1000.

Output

For each case, output the highest possible value of the necklace.

Sample Input

1
2 1
1 1
1 1
3 

Sample Output

1 

第一种、01-背包变形:

#include <cstdio>
#include <cstring>
int max(int x, int y)
{
	return x>y?x:y;
}
int dp[1017][21];
int main()
{
	int W,val[1017],wei[1017];
	int n, k, t, i, j, l;

	while(~scanf("%d",&t))
	{
		while(t--)
		{
			memset(dp,0,sizeof(dp));
			scanf("%d %d",&n,&k);
			for(i = 0 ;i < n; i++)
			{
				scanf("%d %d",&val[i],&wei[i]);
			}
			scanf("%d",&W);
			for(i = 0; i < n; i++)
			{
				for(l = W; l >= wei[i]; l--)
				{
					for(j = 1; j <= k; j++)
					{
						dp[l][j] = max(dp[l][j],dp[l-wei[i]][j-1]+val[i]);
					}
				}
			}
			printf("%d\n",dp[W][k]);
		}
	}
	return 0;
}

第二种、DFS:

待更新……

时间: 2024-10-06 14:13:53

hdu 2660 Accepted Necklace(01-背包变形 || DFS)的相关文章

【DP】 HDU 2660 Accepted Necklace 限制背包

给出n个物品 最多能拿k个 选取的物品的总重量不能超过w 因为每个物品只有一个 转移顺序为 for(int i=0; i<n; i++) for(int j=w; j>=b[i]; j--) for(int l=1; l<=k; l++) 保证了一个物品只放进一次 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <

hdu 2660 Accepted Necklace (二维背包)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2530    Accepted Submission(s): 999 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

HDU 2660 Accepted Necklace (DFS)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2474    Accepted Submission(s): 973 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

hdu - 2660 Accepted Necklace (二维费用的背包问题)

http://acm.hdu.edu.cn/showproblem.php?pid=2660 f[v][u]=max(f[v][u],f[v-1][u-w[i]]+v[i]; 注意中间一层必须逆序循环. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int dp[21][1001]; 6 int v[1001],w[1001]; 7 int

HDU 2660 Accepted Necklace

此题的大意就是要制作一条项链,这个项链必须用k块石头来制作,且重量不能超过W,问所能制作项链的最大价值. 此题数据很水,DFS完全暴力就能过. 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #define MAX(A,B) (A>B?A:B) 5 int n, k, W;//n块石头 制作一条项链所需要的宝石数量k 最大承受重量 W 6 int v[35], w[35], ans;//每块

HDU 2546 饭卡 01背包变形

题目大意:中文题就不多说了 题目思路:由题意可知,只要高于5元,就可以随便刷,那我们就把最贵的留在最后刷.但是如果低于5元就什么也不能刷(哪怕你要买的物品价格不足五元),所以我们可以先求出(n-5)元的情况下最多能花掉多少钱,最后再减去最贵的物品价格就可以了,具体看代码吧. #include<cstdio> #include<stdio.h> #include<cstdlib> #include<cmath> #include<iostream>

HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

codeforce Gym 101102A Coins (01背包变形)

01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAXW 15005 #define N 155 #define LL long long #define MOD 1000000007 int w1[N],w2[N]; LL dp1[MAXW],dp2[MAXW]; int main(