背包问题——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. Unity Tiny & ECS 学习笔记

    1.官方文档 https://docs.unity3d.com/Packages/com.unity.tiny@0.13/manual/intro-for-unity-developers.html ...

  2. C#多线程处理

    创建多线程,并带参数! using System; using System.Collections; using System.Collections.Generic; using System.I ...

  3. github 远程库

    一.在 Github 创建 django 项目时:先在本地创建项目,然后设置为本地仓库,再与远程仓库关联 在 Git Bash 进入django项目目录,输入命令git init,此时这个目录变成Gi ...

  4. python&django 实现页面中关联查询小功能(中级篇)

    目的 组合搜索实现如下图功能 知识点 1.使用自定义标签模板(templatetags) 实现 models.py 和 views.py和初级篇一样 重点如下:在app01目录下创建templatet ...

  5. git本机服务器配置(四):git+TortoiseGit+gitblit配置本机服务器

    1.配置本机git服务器 1.1 打开gitblit服务器,登录之前设置的服务页面localhost:1081 1.2.登录账号,账号在(三)中有提到. 1.3 打开用户中心 1.4 点击SSH Ke ...

  6. Git管理源代码

    Git Git 是目前世界上最先进的分布式版本控制系统(没有之一) 作用 源代码管理 为什么要进行源代码管理? 方便多人协同开发 方便版本控制 Git单人本地仓库操作 安装git sudo apt-g ...

  7. openGL learning

    1,basic env in linux : cmake_minimum_required(VERSION 2.8) project(CP_01) set(GLFW_HOME /home/gearsl ...

  8. C#学习笔记——MDI窗体(多文档界面)

    1.设置父窗体: 如果要将某个窗体设置为父窗体,只需将该窗体的IsMdiContainer属性设置为True即可. 2.设置子窗体: 通过设为某个窗体的MdiParent属性来确定该窗体是那个窗体的子 ...

  9. mingw-gcc-9.0.1-i686-posix-sjlj-201903

    -------------------------------------------------------------------------------gcc version 9.0.1 201 ...

  10. ubuntu server 16.04 安装过程中提示无法安装busybox-initramfs

    这个问题在安装desktop版本时是不会出现的,只有server才有这个问题. 出现这个问题与硬件平台无关,不管是虚拟机还是物理机都会出现,解决的办法是在安装开始界面选择English,后面Langu ...