您的位置:首页 > 其它

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.
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: