背包问题——dfs

问题描述

 

解决思路

采用DFS搜索  其实也是回溯法

代码实现

#include<iostream>
#include<vector>
using namespace std;
struct goods
{
int w;
int v;
int flag;
};
vector<goods> g;
vector<goods> result;
int n; //货物数
int space; //空间
int nowspa; //剩余空间
int nowval; //当前价值
int maxval=0; //最大价值
void bfs(int i, int nowspa, int nowval);
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
cin>>n;
cin>>space;
for(int i=0; i<n; i++)
{
goods temp;
temp.flag=1;
cin>>temp.w;
g.push_back(temp);
} for(int i=0; i<n; i++ )
{
cin>>g[i].v;
}
vector<goods>::iterator it=g.begin();
for(;it!=g.end(); it++)
{
goods temp=*it;
cout<<temp.v<<endl;
cout<<temp.w<<endl;
cout<<temp.flag<<endl;
}
bfs(0, space, 0);
cout<<"maxval="<<maxval<<endl;
cout<<"装载方案"<<endl;
it=result.begin();
for(;it!=result.end(); it++)
{
goods temp=*it;
cout<<temp.flag<<endl;
}
return 0;
}
void bfs(int i, int nowspa, int nowval)
{
if(i==n)
{
if(maxval<nowval)
{
maxval=nowval;
result=g;
}
return ;
}
//装
if(nowspa>=g[i].w)
{
g[i].flag=0;
bfs(i+1,nowspa-g[i].w, nowval+g[i].v);
}
g[i].flag=1;
bfs(i+1,nowspa, nowval);
}

编写中的bug

bug1

 

花了好长时间才解决的,具体原因不明确

以下为解决方案:

 

运行结果

测试用例

运行结果

总结

可以结合王晓东  《算法设计与分析》 中的回溯法一章进行研究。

背包问题——dfs的更多相关文章

  1. 01背包问题(dfs+剪枝)

    01背包问题 dfs解法 #include <iostream> #include <cstring> #include <algorithm> #include ...

  2. 【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序

    题面 100 注意到ban的只会是一个子树,所以我们把原树转化为dfs序列. 然后题目就转化为,询问一段ban的区间,之后的背包问题. 比赛的时候,我想到这里,于是就开始想区间合并,于是搞了线段树合并 ...

  3. 「10.19」最长不下降子序列(DP)·完全背包问题(spfa优化DP)·最近公共祖先(线段树+DFS序)

    我又被虐了... A. 最长不下降子序列 考场打的错解,成功调了两个半小时还是没A, 事实上和正解的思路很近了,只是没有想到直接将前$D$个及后$D$个直接提出来 确实当时思路有些紊乱,打的时候只是将 ...

  4. dp或dfs(01背包问题)

    链接:https://ac.nowcoder.com/acm/contest/993/C来源:牛客网题意:n头牛,给出它们的H高度,问这些牛的高度叠加起来大于等于书架高度,问叠加后的高度与书架的差值最 ...

  5. lintcode:背包问题

    背包问题 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 样例 如果有4个物品[2, 3, 5, 7] 如果背包的大小为,可以选择的空间. 如果背包的大小 ...

  6. POJ 1417 True Liars(种类并查集+dp背包问题)

    题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...

  7. 【DFS】NYOJ-325-zb的生日

    [题目链接:NYOJ-325] 一道以我名字命名的题目,难道要我生日的时候再A? 思路:依旧深搜,但这个问题应该有一个专有名词吧,看别的博客说是 “容量为 sum/2 的背包问题”,不懂... // ...

  8. [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)

    题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...

  9. 杭电OJ——1011 Starship Troopers(dfs + 树形dp)

    Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...

随机推荐

  1. (Python2)自动对话机器人 代码

    dict = { 'hello': 'hello'}flag = 'c'work = Trueprint 'hi,my name is python.'print 'do you want chat ...

  2. Thrax-构建基于语法的语言模型工具

    安装 http://www.openfst.org/twiki/bin/view/GRM/ThraxQuickTour http://cslu.ogi.edu/~sproatr/Courses/Tex ...

  3. PowerPoint 中插入 Latex 公式

    做 PPT 用 Latex Beamer 毕竟还是太麻烦,Beamer 毕竟还是更适合学术性的,各种定义各种公式的那种,遇到要画各种图,插入各种图片,进行错综复杂的排版就比较棘手了. 最终还是 Pow ...

  4. java实现单链接的几种常用操作

    public class ListNode { public int value; public ListNode next; public ListNode(int value) { this.va ...

  5. python装饰器的wraps作用

    不加: from functools import wraps def my_decorator(func): def wper(*args, **kwargs): '''decorator''' p ...

  6. 标准库类型string

    定义和初始化string对象 初始化string对象方式: string s1;//默认初始化,s1是一个字符串 string s2(s1);//s2是s1的副本 string s2 = s1;//等 ...

  7. nodejs分离html文件里面的js和css

    摘要: 本文要实现的内容,使用nodejs 对文件的增删改查,演示的例子->分离出一个html 文件里面的script 和style 里面的内容,然后单独生成js文件和css 文件.中间处理异步 ...

  8. 手把手教你写vue插件并发布(二)

    前记:上一篇 https://www.cnblogs.com/adouwt/p/9211003.html, 说到了一个完整的vue插件开发.发布的流程,总结下来就讲了这么一个事,如何注入vue, 如果 ...

  9. Gitlab_ansible_jenkins三剑客⑥Jenkins和ansible集成

    ip 角色 备注 10.11.0.215 jenkins服务器 通过deploy运行jenkins服务,deploy用户做了免秘钥登录ansible服务器 10.11.0.210 ansible服务器 ...

  10. .Net Core----关于MVC中TempData持久化问题

    最近在做mvc跨控制器传值的时候发现一个问题,就是有时候TempData的值为null,然后查阅了许多资料,发现了许多都是逻辑和原理什么的(想看原理可以查看原理的文章,本文是用法),但是真正解决的办法 ...