背包问题——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. 安卓ADB学习笔记

    ADB(Android Debug Bridge)可以远程调试安卓设备,包括模拟器,可以进入终端模式(安卓本身相当于一个linux) 1.配置adb环境变量 以夜神模拟器为例,将模拟器安装路径里的bi ...

  2. 20155324《网络对抗》Exp2 后门原理与实践

    20155324<网络对抗>Exp2 后门原理与实践 20155324<网络对抗>Exp2 后门原理与实践 常用后门工具实践 Windows获得Linux Shell 在Win ...

  3. linux下安装svn1.7

    转自 https://blog.csdn.net/u011752559/article/details/11559573?locationNum=11&fps=1 1.下载svn安装包 wge ...

  4. 高阶函数(Higher-order function)

    变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下代码: >>> abs(-15) 15 但是,如果只写abs呢? >>> abs ...

  5. vue路由--网站导航功能

    1.首先需要按照Vue router支持 npm install vue-router然后需要在项目中引入: import Vue from 'vue' import VueRouter from ' ...

  6. windows powershell上批量修改文件名称

    $i = Get-ChildItem -Path c:\pictures -Filter *.jpg | ForEach-Object { $extension = $_.Extension $new ...

  7. PostgreSQL学习笔记(二)-安装pgAdmin

    继上篇安装PostgreSQL后,我们需要安装一个PostgreSQL的图形化管理工具. pgadmin管理工具 创建Python的虚拟环境 cd /root/venv python -m venv ...

  8. golang interface类型转string等其他类型

    inter 是interface类型,转化为string类型是: str := inter .(string) 转为其他类型也类似

  9. mac配置变量失败导致ls命令都失效

    1.在命令行中输入 export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 这样可以保证命令行命令暂时可以使用.命令执行完之后先不要关闭终端 ...

  10. Codeforces Round #352 (Div. 2) (A-D)

    672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...