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 <= ), 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<=), representing the value of each precious stone, and b (b<=), its weight.
The last line of each case contains an integer W, the maximum weight my mother will accept, W <= .
Output
For each case, output the highest possible value of the necklace.
 
Sample Input

Sample Output

 
Source
 
dfs暴搜,不断剪枝就过了,不过在

for(int i=cur;i<n;i++){
if(!vis[i]){
vis[i]=1;
dfs(i,num+1,weight+w[i],value+v[i]);
vis[i]=0;
}
}

这里一开写的是for(int i=cur+1;i<n;i++)就错了,改成for(int i=cur;i<n;i++)就莫名其妙地A了。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1<<26
int n,k,total;
int ans;
int v[N];
int w[N];
int vis[N];
void dfs(int cur,int num,int weight,int value){
if(weight>total){
return;
}
int sum=;
for(int i=cur+;i<n;i++){
sum+=v[i];
}
if(value+sum<ans){
return;
}
if(num==k){
ans=max(ans,value);
return;
}
for(int i=cur;i<n;i++){
if(!vis[i]){
vis[i]=;
dfs(i,num+,weight+w[i],value+v[i]);
vis[i]=;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
for(int i=;i<n;i++){
scanf("%d%d",&v[i],&w[i]);
}
scanf("%d",&total);
memset(vis,,sizeof(vis));
ans=-inf;
dfs(,,,);
printf("%d\n",ans);
}
return ;
}

hdu 2660 Accepted Necklace(dfs)的更多相关文章

  1. HDOJ(HDU).2660 Accepted Necklace (DFS)

    HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...

  2. hdu 2660 Accepted Necklace

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...

  3. HDU 2660 Accepted Necklace【数值型DFS】

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

  4. hdu 1716 排序2(dfs)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  6. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  7. HDU 5952 Counting Cliques(dfs)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 1728 逃离迷宫(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)  ...

  9. HDU 6351 Beautiful Now(DFS)多校题解

    思路:一开始对k没有理解好,题意说交换k次,如果我们不需要交换那么多,那么可以重复自己交换自己,那么k其实可以理解为最多交换k次.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位 ...

随机推荐

  1. Farming

    Problem Description You have a big farm, and you want to grow vegetables in it. You're too lazy to s ...

  2. UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>

    D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. Merge Two Sorted Lists 解答

    Question Merge two sorted linked lists and return it as a new list. The new list should be made by s ...

  4. LeeCode(Database)-Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  5. 剑指offer-面试题6.重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历结果,请重建出该二叉树.假设 输入的前序遍历和中序遍历的结果都不含重复的数字.例如输入前序遍历 序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...

  6. OpenWrt编译

    OpenWrt编译简单过程1,OpenWrt编译环境准备sudo apt-get install gcc g++ binutils patch bzip2 flex bison make autoco ...

  7. 【转】android 电容屏(一):电容屏基本原理篇

    关键词:android  电容屏 tp  ITO 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV310(samsung exynos 42 ...

  8. AutoResetEvent与ManualResetEvent区别

    本文来自:http://www.360doc.com/content/10/1126/10/3267996_72536817.shtml 在.Net多线程编程中,AutoResetEvent和Manu ...

  9. IOS将UIView转化为UIImage

    +(UIImage*)createImageFromView:(UIView*)view { //obtain scale CGFloat scale = [UIScreen mainScreen]. ...

  10. js的for in循环和java里的foreach循环的差别

    js里的for in循环定义例如以下: for(var variable in obj) { ... } obj能够是一个普通的js对象或者一个数组.假设obj是js对象,那么variable在遍历中 ...