nyoj289苹果(广搜解01背包问题)
2014-11-18 19:21
274 查看
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=289运行结果:MemoryLimitExceeded代码如下:
#include<cstdio>
02.
#include<cstring>
03.
#include<queue>
04.
using
namespace
std;
05.
int
n;
06.
int
m;
07.
int
best;
08.
int
v[1010];
09.
int
w[1010];
10.
struct
node
11.
{
12.
int
c;
//当前的总价值
13.
int
w;
//当前的总容量
14.
int
s;
//标记当前选到了哪个物品
15.
}n1,n2;
16.
void
bfs()
17.
{
18.
queue<node>q;
19.
n1.c=0;
20.
n1.w=0;
21.
n1.s=0;
22.
q.push(n1);
23.
while
(!q.empty())
24.
{
25.
n2=q.front();
26.
//printf("**%d %d %d**\n",n2.s,n2.w,n2.c);
27.
if
(n2.c>best)
28.
best=n2.c;
29.
q.pop();
30.
if
(n2.s<=n)
31.
{
32.
n1.s=n2.s+1;
33.
if
(n2.w+w[n2.s]<=m)
34.
{
35.
n1.w=n2.w+w[n2.s];
36.
n1.c=n2.c+v[n2.s];
37.
q.push(n1);
38.
}
39.
n1.w=n2.w;
40.
n1.c=n2.c;
41.
q.push(n1);
42.
}
43.
}
44.
45.
}
46.
int
main()
47.
{
48.
while
(
scanf
(
"%d%d"
,&n,&m)!=EOF&&(m+n))
49.
{
50.
for
(
int
i=0;i<n;i++)
51.
scanf
(
"%d%d"
,&w[i],&v[i]);
52.
best=0;
53.
bfs();
54.
printf
(
"%d\n"
,best);
55.
}
56.
return
0;
57.
}
相关文章推荐
- NYOJ 289 苹果(01背包问题)
- NYOJ289 苹果(经典的01背包问题)
- NYOJ 289 苹果 (dp问题之01背包)
- nyoj289苹果(深搜解01背包问题)
- nyoj289-苹果,01背包问题
- nyoj289 苹果 典型01背包问题(动态规划)思路及实现过程
- nyoj 289 苹果(01背包一维和二维实现)
- nyoj-289-苹果(01背包)
- NYOJ - 苹果(dp-01背包问题)
- NYoj_DP_289苹果(01背包)
- 01背包问题 NYOJ苹果
- nyoj--289--苹果(01背包)
- NYOJ289:苹果(01背包入门)
- nyoj--289--苹果(01背包)
- nyoj289苹果(01背包)
- nyoj 289苹果 01背包
- NYOJ 289 苹果(01背包)两种解法,二维数组,一维数组
- nyoj 苹果 289 (简单01背包)
- nyoj 289 苹果 (01背包)
- NYOJ289 苹果 【01背包】