比赛 HAOI2009 模拟试题1 评测结果 WWWEEEEEEE
题目名称 洞窟探索 最终得分 0
用户昵称 苏轼 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-04-21 11:26:52
显示代码纯文本
{
 haoi2009  moni1 t2 hole
 dfs rp++ tree DP
 time:2009.4.21
}
program cch(input,output);
const
 maxf=10000000;
var
 f:array[0..200,0..100] of longint;
 root,n,m,ans:longint;
 rchild,lchild:array[0..200] of integer;
 map:array[0..200,0..200] of longint;
 flag:array[0..200] of boolean;

procedure init;
var
 i,j,x,y,l:longint;
begin
 assign(input,'hole.in');
 assign(output,'hole.out');
 reset(input);
 rewrite(output);
 readln(n,m);
 for i:=1 to n do
  for j:=1 to n do
   map[i,j]:=0;
 for i:=1 to n-1 do
  begin
   readln(x,y,l);
   map[x,y]:=l; map[y,x]:=l;
  end;
 for i:=0 to n do
  for j:=0 to m do
   f[i,j]:=-1;
 for i:=1 to n do
  f[i,1]:=0;
 for i:=1 to m do f[0,i]:=maxf;
end;

function small(x,y:longint):longint;
begin
 if x<y then exit(x)
  else exit(y);
end;

function dfs(k,p:longint):longint;
var
 i,min:longint;
begin
 if f[k,p]<>-1 then exit(f[k,p]);
 min:=maxf;
 for i:=1 to p-2 do
  min:=small(min,dfs(lchild[k],i)+dfs(rchild[k],p-i-1)+(map[k,lchild[k]]+map[k,rchild[k]])*(p-1));
 min:=small(min,dfs(lchild[k],p-1)+map[k,lchild[k]]*(p-1));
 min:=small(min,dfs(rchild[k],p-1)+map[k,rchild[k]]*(p-1));
 f[k,p]:=min;
 exit(min);
end;

procedure maketree(k:longint);
var
 i:longint;
begin
 flag[k]:=false;
 lchild[k]:=0; rchild[k]:=0;
 for i:=1 to n do
  if (map[k,i]>0)and(flag[i]) then
   begin
    if lchild[k]=0 then lchild[k]:=i
                   else rchild[k]:=i;
    maketree(i);
   end;
end;

procedure main;
var
 i:longint;
begin
 root:=1;
 for i:=1 to n do flag[i]:=true;
 maketree(root);
 ans:=dfs(root,m);
end;

procedure print;
var
 ans1:real;
begin
 m:=(m-1)*m div 2;
 ans1:=ans*2/m;
 writeln(ans1:0:2);
 close(input);
 close(output);
end;

begin
 init;
 main;
 print;
end.