program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
MaxN = 255;
type
TArticle = record
w: Integer;
v: Integer;
OldIndex: Integer;
p:do
uble
end;
TNode = record
d: Integer;
UsedArticles: set of Byte;
v: Integer;
p: Integer
end;
var
N: Integer;
P: Integer;
Articles: array [1..MaxN] of TArticle;
Top: Integer;
Stack: array [1..MaxN] of TNode;
Node: TNode;
Best: array [1..MaxN] ofdo
uble;
BetterSet: set of Byte;
BetterV: Integer;
procedure Init;
var
i, j: Byte;
t: TArticle;
PP: Integer;
begin
Write('N: ');
ReadLn(N);
for i:=1 to Ndo
begin
Write('w[', i, ']:');
ReadLn(Articles.w);
Write('v[', i, ']:');
ReadLn(Articles.v);
Articles.p:=Articles.v/Articles.w;
Articles.OldIndex:=i
end;
Write('P: ');
ReadLn(P);
for i:=1 to N-1do
for j:=i+1 to Ndo
if Articles.p<Articles[j].p then
begin
t:=Articles;
Articles:=Articles[j];
Articles[j]:=t
end;
Best[1]:=0;
for i:=2 to Ndo
Best[1]:=Best[1]+Articles.v;
for i:=2 to Ndo
Best:=Best[i-1]-Articles.v;
PP:=P;
BetterSet:=[];
BetterV:=0;
for i:=1 to Ndo
if Articles.w<=PP then
begin
BetterSet:=BetterSet+;
BetterV:=BetterV+Articles.v;
PP:=PP-Articles.w
end;
Top:=1;
Stack[Top].d:=0;
Stack[Top].UsedArticles:=[];
Stack[Top].v:=0;
Stack[Top].p:=P
end;
procedure Print;
var
i: Integer;
begin
Write('Sum: ', BetterV,'. ');
for i:=1 to Ndo
if (i in BetterSet) then
Write(' ', Articles.OldIndex);
ReadLn
end;
begin
Init;
while Top>0do
begin
while Stack[Top].d<Ndo
begin
Inc(Stack[Top].d);
if Articles[Stack[Top].d].w<=Stack[Top].p then
begin
Node:=Stack[Top];
Node.UsedArticles:=Node.UsedArticles+[Node.d];
Node.v:=Node.v+Articles[Node.d].v;
Node.p:=Node.p-Articles[Node.d].w;
if Top=N then
begin
if Node.v>BetterV then
begin
BetterV:=Node.v;
BetterSet:=Node.UsedArticles
end
end
else
if (Node.v+Best[Node.d])>BetterV then
begin
if Node.v>=BetterV then
begin
BetterV:=Node.v;
BetterSet:=Node.UsedArticles
end;
Inc(Top);
Stack[Top]:=Node
end
end
end;
Dec(Top)
end;
Print
end.
找到Bug请速通知我.