poj 1789 Truck History 最小生成树

题意:历史上,曾用7个小写字母来表示每种truck的型号,每两种型号之间的差距为字母串中不同字母的个数。现在给出n种不同型号的truck,问怎样使

1/Σ(to,td)d(to,td)的值最小。(即找到一条连接所有truck的最短路径。典型的最小生成树的问题。

不多说,直接附代码:

var
  n,i,j,k,ans:longint;
  a:array[1..2000] of string;
  f:array[1..2000,1..2000] of longint;
  d:array[1..2000] of longint;
  v:array[1..2000] of boolean;

procedure dij;
var
  u,min,i:longint;
begin
  fillchar(v,sizeof(v),true);
  fillchar(d,sizeof(d),$7f div 10);
  d[1]:=0;
  repeat
    u:=0;
    min:=maxlongint;
    for i:=1 to n do
      if (d[i]<min)and(v[i]) then
      begin
        min:=d[i];
        u:=i;
      end;
    if u>0 then
    begin
      ans:=ans+min;
      v[u]:=false;
      for i:=1 to n do
        if (v[i])and(f[u,i]<d[i]) then d[i]:=f[u,i];
    end;
  until u=0;
end;

begin
  readln(n);
  while n>0 do
  begin
    for i:=1 to n do
      readln(a[i]);
    fillchar(f,sizeof(f),0);
    for i:=1 to n-1 do
      for j:=i+1 to n do
        for k:=1 to 7 do
          if a[i,k]<>a[j,k] then
          begin
            inc(f[i,j]);
            inc(f[j,i]);
          end;
    ans:=0;
    dij;
    writeln('The highest possible quality is 1/',ans,'.');
    readln(n);
  end;
end.


你可能感兴趣的:(poj 1789 Truck History 最小生成树)