比赛 HAOI2009 模拟试题1 评测结果 AWWEEEEEEE
题目名称 洞窟探索 最终得分 10
用户昵称 lc 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-04-21 10:45:37
显示代码纯文本
program hole;
 const
    Inf=1000000;
    maxn=150; maxm=100;
  var
      n,m:      longint;
      visit:    array[1..maxn] of boolean;
      l:        array[1..maxn,0..2] of longint;
      g:        array[1..maxn,0..maxm] of longint;
      opt:      array[0..maxn,0..maxm] of longint;
      Ans:      longint;


 procedure built(v:longint);
   var
       i:       longint;
   begin
     visit[v] :=true;
     for i :=1 to n do if g[v,i]>0
         then begin
              if visit[i] then continue;
              inc(l[v,0]); l[v,l[v,0]]:=i;
              built(i);
              end;
   end;


 procedure Init;
   var
      i,x,y,u:        longint;

  begin
    readln(n,m);
    for i :=1 to n-1 do begin
        readln(x,y,u);
        g[x,y] :=u;
        g[y,x] :=u;
        end;
    built(1);
  end;

 function solve(x,y:longint):longint;
   var
       tmp,tm,k,Lx,Rx:  longint;
   begin
     if (x=0) and (y<>0) then exit(Inf);
     if (x=0) and (y=0)  then exit(0);
     if opt[x,y] <> -1 then exit(opt[x,y]);
     if  l[x,0]=0 then begin opt[x,y] :=0; exit(opt[x,y]) end;
     tmp := Inf;
     lx :=l[x,1]; rx :=l[x,2];

     for k :=0 to y-1 do begin
         tm :=solve(lx,k)+solve(rx,y-1-k)+k*(y-1-k)*(g[x,lx]+g[x,rx])+
              g[x,lx]*k+g[x,rx]*(y-1-k)+k*(m-y)*g[x,lx]+(y-k-1)*(m-y)*g[x,rx];
         if tm < tmp then tmp :=tm;
         end;
     for k :=0 to y do begin
         tm :=solve(lx,k)+solve(rx,y-k)+k*(y-k)*(g[x,lx]+g[x,rx])+k*g[x,lx]+(y-k)*g[x,rx]
         +k*(m-y)*g[x,lx]+(y-k)*(m-y)*g[x,rx];
         if tm <tmp then tmp :=tm;
         end;
     opt[x,y] :=tmp;
     exit(opt[x,y]);
   end;


 procedure Main;

   begin
     fillchar(opt,sizeof(opt),$FF);
     Ans :=solve(1,m);
     writeln(Ans/m:0:2);
   end;

 begin
   assign(input,'hole.in'); reset(input);
   assign(output,'hole.out'); rewrite(output);
   Init;
   Main;
   close(input); close(output);
 end.