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. 在Linux CentOS 6.5 (Final)上安装git-1.9.0

    CentOS 6.5 (Final)默认安装的git版本为1.7.1.3,而我们希望安装1.9.0版本.由于rpm安装库里没有1.9.0版本,因此我们需要找其它方法来安装. 网上有很多文章介绍了如何从 ...

  2. Jquery时间验证和转换工具

    var TimeObjectUtil; /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * ...

  3. java Socket使用注意

    Socket s = new Socket(ia, port); BufferedOutputStream bufOut = new BufferedOutputStream(s.getOutputS ...

  4. SVN在ubuntu的安装和使用

    安装: svn客户端:apt-get install subversion ,然后根据提示一步一步,就完成了svn的安装.当然,也可以源码安装 svn,下载 subversion 一个最新版本的源码包 ...

  5. windows下启动/关闭Sybase数据库服务器

    启动.关闭Sybase数据库服务器 一.启动Sybase服务器 在windows下介绍两种方法启动Sybase数据库服务器. 1.通过服务器管理器 依次打开控制面板>管理工具>服务 管理窗 ...

  6. stagefright框架(三)-选择Video Decode

    在<Stagefright (1) – Video Playback的流程>中,我们并没有详述Stagefright是如何根据影片档的类型来选择适合的video decoder,现在,就让 ...

  7. Entity Framework with MySQL

    Get Entity Framework: http://msdn.microsoft.com/en-us/data/ee712906 Entity Framework 6 Tools for Vis ...

  8. eclipse美化,全黑eclipse 保护眼睛

    如标题,闲话不多说.有图说明一切.看看这是你想要的嘛? 教程及资源下载地址: http://download.csdn.net/detail/shoneworn/8326097

  9. jquery创建动态的div

    参考:http://blog.csdn.net/ge_zhiqiang/article/details/6958230

  10. POJ 2446 Chessboard

    要求用占两格的长方形铺满平面上除去指定点 二分图匹配 #include <iostream> #include <cstdio> #include <cstring> ...