比赛 HAOI2009 模拟试题1 评测结果 AAAAAAAAAA
题目名称 洞窟探索 最终得分 100
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-03-25 21:17:24
显示代码纯文本
  1. program hole;
  2. type
  3. dian=record
  4. first,vdata:longint;
  5. end;
  6. bian=record
  7. v,next,w:longint;
  8. end;
  9. dptree=record
  10. left,right,lw,rw,vdata:longint;
  11. end;
  12.  
  13. var
  14. list:array[0..1500] of dian;
  15. map:array[0..3000] of bian;
  16. tree:array[0..1500] of dptree;
  17. f:array[0..1500,0..1000] of longint;
  18. b:array[0..1500] of boolean;
  19. n,m,i,j,r1,r2,r3,ans:longint;
  20.  
  21. function max(x,y:longint):longint;
  22. begin
  23. if x>y
  24. then max:=x
  25. else max:=y;
  26. end;
  27.  
  28. function min(x,y:longint):longint;
  29. begin
  30. if x<y
  31. then min:=x
  32. else min:=y;
  33. end;
  34.  
  35. procedure maketree(v:longint);
  36. var
  37. i:longint;
  38. begin
  39. tree[v].vdata:=1;
  40. i:=list[v].first;
  41. while i<>0 do
  42. begin
  43. if b[map[i].v]=false then
  44. begin
  45. if tree[v].left=0 then
  46. begin
  47. tree[v].left:=map[i].v;
  48. tree[v].lw:=map[i].w;
  49. b[map[i].v]:=true;
  50. maketree(map[i].v);
  51. tree[v].vdata:=tree[v].vdata+tree[tree[v].left].vdata;
  52. end
  53. else
  54. begin
  55. tree[v].right:=map[i].v;
  56. tree[v].rw:=map[i].w;
  57. b[map[i].v]:=true;
  58. maketree(map[i].v);
  59. tree[v].vdata:=tree[v].vdata+tree[tree[v].right].vdata;
  60. end;
  61. end;
  62. i:=map[i].next;
  63. end;
  64. end;
  65.  
  66. function dp(i,j:longint):longint;
  67. var
  68. i1,num1:longint;
  69. begin
  70. if (tree[i].vdata=1) or (j=0) then exit(0);
  71. if f[i,j]<>-1 then exit(f[i,j]);
  72.  
  73. if tree[i].right=0 then
  74. begin
  75. f[i,j]:=dp(tree[i].left,j-1)+(j-1)*(m-(j-1))*tree[i].lw;
  76. end
  77. else
  78. begin
  79. f[i,j]:=maxlongint;
  80. for i1:=max(0,j-tree[tree[i].right].vdata-1) to min(j-1,tree[tree[i].left].vdata) do
  81. begin
  82. if i1>tree[tree[i].left].vdata then continue;
  83. if j-1-i1>tree[tree[i].right].vdata then continue;
  84. num1:=dp(tree[i].left,i1)+i1*(m-i1)*tree[i].lw
  85. +dp(tree[i].right,j-1-i1)+(j-1-i1)*(m-(j-1-i1))*tree[i].rw;
  86. if num1<f[i,j] then f[i,j]:=num1;
  87. end;
  88. end;
  89. dp:=f[i,j];
  90. end;
  91.  
  92. begin
  93. assign(input,'hole.in');
  94. reset(input);
  95. assign(output,'hole.out');
  96. rewrite(output);
  97.  
  98. readln(n,m);
  99. for i:=0 to n do
  100. for j:=0 to m do
  101. f[i,j]:=-1;
  102. fillchar(list,sizeof(list),0);
  103. for i:=1 to n-1 do
  104. begin
  105. readln(r1,r2,r3);
  106.  
  107. map[i*2-1].v:=r2;
  108. map[i*2-1].w:=r3;
  109. map[i*2-1].next:=list[r1].first;
  110. list[r1].first:=i*2-1;
  111. inc(list[r1].vdata);
  112.  
  113. map[i*2].v:=r1;
  114. map[i*2].w:=r3;
  115. map[i*2].next:=list[r2].first;
  116. list[r2].first:=i*2;
  117. inc(list[r2].vdata);
  118. end;
  119.  
  120. fillchar(b,sizeof(b),false);
  121. b[1]:=true;
  122. maketree(1);
  123.  
  124. fillchar(f,sizeof(f),$FF);
  125. ans:=dp(1,m);
  126. writeln(ans/(m*(m-1)/2):0:2);
  127.  
  128. close(input);
  129. close(output)
  130. end.
  131.