还是个MST,不过这个读入数据让用Pascal的童鞋很郁闷的说……
Var
a,b,e,f:array[1..100000]of longint;
n,m,sum,ans:longint;
Procedure addgraph(ch1,ch2:char;cost:longint);
begin
inc(m);
a[m]:=ord(ch1)-64;
b[m]:=ord(ch2)-64;
e[m]:=cost;
end;
Procedure init;
var
i,j,k,cost:longint;
ch,ch2,tmp:char;
begin
m:=0;
sum:=0;
ans:=0;
fillchar(f,sizeof(f),0);
readln(n);
if n=0 then halt;
for i:=1 to n-1 do
begin
read(ch);
read(tmp);
read(k);
for j:=1 to k do
begin
read(tmp);
read(ch2);
read(tmp);
read(cost);
addgraph(ch,ch2,cost);
end;
readln;
end;
end;
Procedure qsort(l,r:longint);
var
i,j,x,y:longint;
begin
i:=l;j:=r;x:=e[(l+r)shr 1];
Repeat
while e[i]<x do inc(i);
while e[j]>x do dec(j);
if i<=j then
begin
y:=a[i];a[i]:=a[j];a[j]:=y;
y:=b[i];b[i]:=b[j];b[j]:=y;
y:=e[i];e[i]:=e[j];e[j]:=y;
inc(i);dec(j);
end;
Until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
Function find(x:longint):longint;
var
k:longint;
begin
k:=x;
while f[k]<>0 do k:=f[k];
find:=k;
end;
Procedure kruskal;
var
i,j,p,q:longint;
begin
qsort(1,m);
for i:=1 to m do
begin
p:=find(a[i]);
q:=find(b[i]);
if p<>q then
begin
f[q]:=p;
inc(sum,e[i]);
inc(ans);
end;
if ans>=n-1 then break;
end;
writeln(sum);
end;
Begin
n:=1;
while n<>0 do
begin
init;
kruskal;
end;
End.