比赛 HAOI2009 模拟试题3 评测结果 AAAAA
题目名称 医院设置 最终得分 100
用户昵称 苏轼 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-04-23 09:11:51
显示代码纯文本
{
 haoi2009 moni3 t3
 time:2009.4.23
}
program cch(input,output);
const
 maxf=100000000;
var
 map:array[1..100,1..100] of boolean;
 d,data:array[1..100] of longint;
 ans,n:longint;

procedure init;
var
 i,j,x,y,z:longint;
begin
 assign(input,'hospital.in');
 assign(output,'hospital.out');
 reset(input);
 rewrite(output);
 readln(n);
 for i:=1 to n do
  for j:=1 to n do
   map[i,j]:=false;
 for i:=1 to n do
  begin
   readln(x,y,z);
   if y<>0 then
    begin
     map[i,y]:=true; map[y,i]:=true;
    end;
   if z<>0 then
    begin
     map[i,z]:=true; map[z,i]:=true;
    end;
   data[i]:=x;
  end;
end;

procedure dfs(k,q:longint);
var
 i:longint;
begin
 d[k]:=q;
 for i:=1 to n do
  if (map[k,i])and(d[i]=-1) then
    dfs(i,q+1);
end;

procedure main;
var
 i,ch,j:longint;
begin
 ans:=maxf;
 for i:=1 to n do
  begin
   for j:=1 to n do d[j]:=-1;
   dfs(i,0);
   ch:=0;
   for j:=1 to n do
    ch:=ch+d[j]*data[j];
   if ch<ans then ans:=ch;
  end;
end;

procedure print;
begin
 writeln(ans);
 close(input);
 close(output);
end;

begin
 init;
 main;
 print;
end.