hdu 2660 Accepted Necklace(dfs)
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.
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 <= .
For each case, output the highest possible value of the necklace.
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)的更多相关文章
- HDOJ(HDU).2660 Accepted Necklace (DFS)
HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
- HDU 2660 Accepted Necklace【数值型DFS】
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 1716 排序2(dfs)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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 ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU 5952 Counting Cliques(dfs)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 1728 逃离迷宫(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) ...
- HDU 6351 Beautiful Now(DFS)多校题解
思路:一开始对k没有理解好,题意说交换k次,如果我们不需要交换那么多,那么可以重复自己交换自己,那么k其实可以理解为最多交换k次.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位 ...
随机推荐
- poj 2299 Ultra-QuickSort(归并排序或是bit 树+离散化皆可)
题意:给一个数组,计算需要的冒泡排序的次数,元素个数很大,不能用n^2的冒泡排序计算. 解析:这题实际上就是求逆序对的个数,可以用归并排序的方法,我这里用另一种方法写,bit树+离散化.由于元素的值可 ...
- C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\11.0
Generating Files with the TextTransform Utility \Program Files\Common Files\Microsoft Shared\TextTem ...
- find命令笔记
find 命令: 文件查找:locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的:# updatedb, 手动生成文件数据库速度快 find: 实时 精确 支持众 ...
- MonoDevelop with Visual Studio to Linux and Mac OSX maintaining a single code base for all platforms.
Home | Screenshots | Download | Contact | FAQ | Documentation | Development | Search MonoDevelop i ...
- MySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO
MySQL安装完server端和客户端后,登录Mysql时报错:[root@rhel204 MySQL 5.6.23-RMP]# mysqlERROR 2002 (HY000): Can't conn ...
- windows下sqlplus / as sysdba报ora-12560的解决方法
环境:win7_64位.数据库版本ORACLE11G_R2 在CMD窗口,使用下面三个命令可正常连接数据库:C:\Users\Administrator> sqplus /nolog C:\Us ...
- vmware tools 安装
转到虚拟机 > 安装 VMware Tools(或 VM > 安装 VMware Tools).注意:如果您运行的是轻量版的 Fusion.不带 VMware Tools 的 Workst ...
- 安装Php时候报错信息:virtual memory exhausted: Cannot allocate memory (不能分配内存)
原因是fileinfo这个函数在编译时非常消耗内存,而系统内存又不够了,所以才会出现此问题. 网上找了方法: 1,关闭其他占用大内存的进程. 2,在编译是添加参数 --disable-fileinfo
- mysql操作SQL语句
二.数据库操作SQL语句1.显示服务器上当前存在什么数据库SHOW DATABASES; 2.创建名称为rewin的数据库CREATE DATABASE rewin; 3.删除名称为rewin的数据库 ...
- ASP.NET通用权限组件实现一
沙发(SF)通用权限验证组件 开篇 上一篇提到了通用权限的设计思路,根据设计思路一步一步的来实现一个相对通用的权限验证组件.在VS2010下用C#语言基于.net framework2.0框架实现具体 ...