比赛 |
HAOI2009 模拟试题1 |
评测结果 |
AAAAAAAAAA |
题目名称 |
洞窟探索 |
最终得分 |
100 |
用户昵称 |
ybh |
运行时间 |
0.000 s |
代码语言 |
Pascal |
内存使用 |
0.00 MiB |
提交时间 |
2011-03-25 21:17:24 |
显示代码纯文本
program hole;
type
dian=record
first,vdata:longint;
end;
bian=record
v,next,w:longint;
end;
dptree=record
left,right,lw,rw,vdata:longint;
end;
var
list:array[0..1500] of dian;
map:array[0..3000] of bian;
tree:array[0..1500] of dptree;
f:array[0..1500,0..1000] of longint;
b:array[0..1500] of boolean;
n,m,i,j,r1,r2,r3,ans:longint;
function max(x,y:longint):longint;
begin
if x>y
then max:=x
else max:=y;
end;
function min(x,y:longint):longint;
begin
if x<y
then min:=x
else min:=y;
end;
procedure maketree(v:longint);
var
i:longint;
begin
tree[v].vdata:=1;
i:=list[v].first;
while i<>0 do
begin
if b[map[i].v]=false then
begin
if tree[v].left=0 then
begin
tree[v].left:=map[i].v;
tree[v].lw:=map[i].w;
b[map[i].v]:=true;
maketree(map[i].v);
tree[v].vdata:=tree[v].vdata+tree[tree[v].left].vdata;
end
else
begin
tree[v].right:=map[i].v;
tree[v].rw:=map[i].w;
b[map[i].v]:=true;
maketree(map[i].v);
tree[v].vdata:=tree[v].vdata+tree[tree[v].right].vdata;
end;
end;
i:=map[i].next;
end;
end;
function dp(i,j:longint):longint;
var
i1,num1:longint;
begin
if (tree[i].vdata=1) or (j=0) then exit(0);
if f[i,j]<>-1 then exit(f[i,j]);
if tree[i].right=0 then
begin
f[i,j]:=dp(tree[i].left,j-1)+(j-1)*(m-(j-1))*tree[i].lw;
end
else
begin
f[i,j]:=maxlongint;
for i1:=max(0,j-tree[tree[i].right].vdata-1) to min(j-1,tree[tree[i].left].vdata) do
begin
if i1>tree[tree[i].left].vdata then continue;
if j-1-i1>tree[tree[i].right].vdata then continue;
num1:=dp(tree[i].left,i1)+i1*(m-i1)*tree[i].lw
+dp(tree[i].right,j-1-i1)+(j-1-i1)*(m-(j-1-i1))*tree[i].rw;
if num1<f[i,j] then f[i,j]:=num1;
end;
end;
dp:=f[i,j];
end;
begin
assign(input,'hole.in');
reset(input);
assign(output,'hole.out');
rewrite(output);
readln(n,m);
for i:=0 to n do
for j:=0 to m do
f[i,j]:=-1;
fillchar(list,sizeof(list),0);
for i:=1 to n-1 do
begin
readln(r1,r2,r3);
map[i*2-1].v:=r2;
map[i*2-1].w:=r3;
map[i*2-1].next:=list[r1].first;
list[r1].first:=i*2-1;
inc(list[r1].vdata);
map[i*2].v:=r1;
map[i*2].w:=r3;
map[i*2].next:=list[r2].first;
list[r2].first:=i*2;
inc(list[r2].vdata);
end;
fillchar(b,sizeof(b),false);
b[1]:=true;
maketree(1);
fillchar(f,sizeof(f),$FF);
ans:=dp(1,m);
writeln(ans/(m*(m-1)/2):0:2);
close(input);
close(output)
end.