D - DFS HDU - 2660

D - DFS

HDU - 2660

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 

题目描述:

在n个宝石中选k个,使他总价值最大,总重不大于W。

分析:

因为n的数很小,所以用dfs来做。n个宝石构成一个集合,我们要用dfs遍历他的所有子集。只要对于每个宝石,我们选和不选,就构成2^n个集合,就是他的子集。

只要dfs设置好结束条件,和选够k件时更新下最大值max即可。

要设置2个参数,如i+1为选,i为不选。j+1为已经访问该宝石。

加上每个物品价值,重量,时要注意是第j个,而不是第i个。

代码:

#include<iostream>
#include<algorithm>
#define MAX(x,y) x>y?x:y;
using namespace std;
int k,W,n;
int m=0;
int w[30];
int v[30];
int dfs(int i,int sum,int value,int j)
{
    if(sum>W) return 0;
    if(j>n) return 0;
    if(i==k) return m=MAX(m,value);

    dfs(i+1,sum+w[j],value+v[j],j+1);
    dfs(i,sum,value,j+1);

    return 0;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d %d",&n,&k);
        for(int i=0;i<n;i++)
        {
            cin>>v[i];
            cin>>w[i];
        }
        cin>>W;
        m=0;
        dfs(0,0,0,0);
        printf("%d\n",m);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/studyshare777/p/12190105.html

时间: 2024-12-14 05:49:04

D - DFS HDU - 2660的相关文章

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(01-背包变形 || DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2660 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

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

变形课(DFS hdu 1181)

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 19133    Accepted Submission(s): 6892 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

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

(水DFS) hdu 5024

D - Wang Xifeng's Little Plot Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5024 Appoint description:  System Crawler  (2015-04-14) Description <Dream of the Red Chamber>(also <The Sto

DFS HDU 1342

很水的DFS Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 752 Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2

(dfs) hdu 2258

Continuous Same Game (1) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 465    Accepted Submission(s): 176 Problem Description Continuous Same Game is a simple game played on a grid of colored

DFS HDU 2614

很水的.但是wa了很多遍,因为写num[1][1]... 改成0之后就过了...虽然不知道为什么... Beat Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 659    Accepted Submission(s): 415 Problem Description Zty is a man that always full of