HDOJ(HDU).2660 Accepted Necklace (DFS)
HDOJ(HDU).2660 Accepted Necklace (DFS)
题意分析
给出一些石头,这些石头都有自身的价值和重量。现在要求从这些石头中选K个石头,求出重量不超过W的这些石头的最大价值是多少?
类似于之前讨论到的数字选不选的问题,此处面临的情况是石头选不选,若选进行一个dfs,若不选择进行另外一个dfs。考虑递归边界:
1.当选够了K个的时候,终止递归;
2.当当前重量大于W的时候,终止递归;
3.当所选石头的下标(代码中的pos)超过石头数量的时候,终止递归;
若遇到最优的情况,别忘记更新价值的最大值。
代码总览
/*
Title:HDOJ.2660
Author:pengwill
Date:2017-2-15
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define nmax 21
using namespace std;
int stone[nmax][2],n,m,val,ans,mwei;
void dfs(int num ,int nval,int nwei,int pos)
{
if(num==m)
if(nval>ans && nwei<=mwei ){ans = nval;return;}
else return;
if(nwei>mwei) return;
if(pos>n) return;
dfs(num+1,nval+stone[pos][0],nwei+stone[pos][1],pos+1);
dfs(num,nval,nwei,pos+1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
ans = 0;
scanf("%d%d",&n,&m);
for(int i = 1; i<=n; ++i) scanf("%d%d",&stone[i][0],&stone[i][1]);
scanf("%d",&mwei);
dfs(0,0,0,1);
printf("%d\n",ans);
}
return 0;
}
HDOJ(HDU).2660 Accepted Necklace (DFS)的更多相关文章
- 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 2660 Accepted Necklace(dfs)
Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...
- 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]; 注意中间一层必须逆序循环 ...
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- hdu2660 Accepted Necklace (DFS)
Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...
- HDOJ(HDU).2266 How Many Equations Can You Find (DFS)
HDOJ(HDU).2266 How Many Equations Can You Find (DFS) [从零开始DFS(9)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零 ...
随机推荐
- iTextSharp动态生成多页pdf及追加内容等记录
1.要动态生成pdf,无非是用第三方或直接代码生成. 2.iTextSharp生成pdf问题点记录 dll相关下载 https://files.cnblogs.com/files/xlgwr/iTex ...
- VS Help Viewer 显示内容为HTML源码的问题
万恶的IE10 为了学习,安装了一套Windows Server 2012+SQL 2012+VS 2012的环境,整体感觉还不错,只是在使用Help Viewer查看帮助的时候,发现显示内容居然为H ...
- 程序员的冷笑话 python版本
在伯乐在线上看到了个冷笑话,感觉很有意思. void tellStory() { printf("从前有座山\n"); printf("山上有座庙\n"); p ...
- python爬虫实例大全
WechatSogou [1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典. DouBanSpider [2]- ...
- 【转】cocos2d-x如何优化内存的应用
原地址:http://cblog.chinadaily.com.cn/blog-942327-4327173.html 注:自身以前也写过cocos2d-x如何优化内存的应用,以及内存不够的情况下怎么 ...
- jmeter使用复习
多终端进程: 配置客户端远程的ip地址和port 在客户端jmeter安装目录的bin目录下,修改配置文件 jmeter.properties 默认的remote_hosts 的值:(将肉鸡的地址加入 ...
- DeepLearning Intro - sigmoid and shallow NN
This is a series of Machine Learning summary note. I will combine the deep learning book with the de ...
- QT打开文件路径中含有中文和空格问题
使用qt-mingw版做的软件,发给客户以后说工作不正常,配置文件无法打开,或者加载数据文件不正常.远程查看以后,发现客户经常将程序放置在中文带空格的路径下,导致文件打开不正常.所以最近想在程序上解决 ...
- Discover the Web(栈模拟)
Description Standard web browsers contain features to move backward and forward among the pages rece ...
- SDUST OJ 时间类的加、减法赋值运算
Problem F: 时间类的加.减法赋值运算 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 3801 Solved: 2210[Submit][St ...