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. HDOJ-1018 Big Number

    http://acm.hdu.edu.cn/showproblem.php?pid=1018 题意:给出一个数n,输出n的阶乘的位数 汗Σ( ° △ °|||)︴刚开始还准备上大数乘法 然而10000 ...

  2. 使用Canvas实现动画效果 | DKlogs -- 设计 | 生活

    使用Canvas实现动画效果 | DKlogs -- 设计 | 生活 使用Canvas实现动画效果

  3. cocos2dx lua 学习笔记(二)

    安装开发环境 sublime - http://www.sublimetext.com/2 package control - http://packagecontrol.io/installatio ...

  4. Android开发之Intent.Action

    1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的開始.比較经常使用. Input:nothing Out ...

  5. hdu 4750 Count The Pairs (2013南京网络赛)

    n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...

  6. java学习笔记day07

    1.throwable下面的子类分为两大类:Error 和 Exception 2.如果方法上有throws Exception,则必须对异常进行处理:  try{    需要检测异常代码     } ...

  7. JAVA HashMap与HashTable 区别

    HashTable和HashMap区别 第一,继承不同. public class Hashtable extends Dictionary implements Mappublic class Ha ...

  8. AUL使用初记

    案例:部门有一个数据库因为机器无故重启,无法启动,初步判断是系统表空间出问题了.尝试过各种不同手段,均无法修复.后来发现上面只有一个用户的数据,遂想到直接通过AUL工具从数据文件中抽取出整个库. 准备 ...

  9. C#中equals方法和==的区别

    Msdn中对equals方法的解释是:确定指定的对象是否等于当前对象. Equals方法是比较对象的内容,而==则是比较整个对象是否相等. Equals方法判断的是堆中的值,而==则判断的是堆栈中的值 ...

  10. 从汇编看c++中全局对象和全局变量

    先来看c++源码: #include <iostream> using namespace std; class X { public: int i; public: X() : i(ii ...