@TT
1561 的代码我有,但是不知道如何判断-1的情况:
var
n,m,i:longint;
fa,w:array [1..200] of longint;
f:array [0..200,0..200] of longint;
procedure dfs(h,s:longint);
var
i,j:longint;
begin
if s<=0 then exit;
for i:=1 to n do
if fa[i]=h then begin
for j:=0 to s-1{cost[child]} do f[i,j]:=f[h,j];
dfs(i,s-1{cost[child]});
for j:=1{cost[child]} to s do
if f[h,j]<f[i,j-1]+w[i] then
f[h,j]:=f[i,j-1]+w[i];
end;
end;
begin
repeat
readln(n,m);
if not ((n=0)and(m=0)) then begin
for i:=1 to n do readln(fa[i],w[i]);
dfs(0,m-0);
writeln(f[0,m-0]);
end;
until (n=0)and(m=0);
end.
回复 更多评论