usaco 2.4 comehome 2008.7.16
{
1.路是双向的,不是单项的。
2.起始时读入数据出错,当读入一条路x[l1,l2]时,要从x[l2,l1]和x[l1,l2]中选择一条小的(当然,都要大于零),否则就不能得到最优的解。这点题目中似乎暗示了,我却没注意到}
{
ID:
PROG:comehome
LANG:PASCAL
}
program p_comehome;
const fin='comehome.in';fout='comehome.out';
nn=52;
var a:array['A'..'Z']of longint;
b:array['a'..'z']of longint;
x:array[1..nn,1..nn]of longint;
ch:string;f1,f2:text;u,op,w:char;
la,k,max,code,s,n,pz,i,j,l,l1,l2,maxnn:longint;
function deal(t:char):longint;
begin
if t<'a' then
begin
if (t>op) and (t<>'Z') then op:=t;
if a[t]=0 then
begin
inc(l);a[t]:=l;
inc(la);
exit(l)
end else exit(a[t])
end
else
begin
if b[t]=0 then
begin
inc(l);b[t]:=l;
exit(l)
end else exit(b[t])
end;
end;
procedure init;
begin
op:='A';la:=0;
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
for i:=1 to nn do for j:=1 to nn do
x[i,j]:=99999999;
assign(f1,fin);reset(f1);
assign(f2,fout);rewrite(f2);
readln(f1,n);l:=0;
for i:=1 to n do
begin
readln(f1,ch);
val(copy(ch,5,length(ch)-4),s,code);
l1:=deal(ch[1]);l2:=deal(ch[3]);
x[l1,l2]:=s;
if x[l2,l1]<x[l1,l2] then x[l1,l2]:=x[l2,l1] else
x[l2,l1]:=x[l1,l2];
end;
end;
procedure choose;
begin
for k:=1 to l do
for i:=1 to l do
if i<>k then
for j:=1 to l do
if (j<>k)and(i<>j)then
if (x[i,j]>x[i,k]+x[k,j])then
x[i,j]:=x[i,k]+x[k,j];
end;
{-----------------------main---------------------------}
begin
max:=maxlongint;
init;
choose;
for w:='A' to 'Y' do
if a[w]<>0 then
begin
if x[a[w],a['Z']]<max then
begin max:=x[a[w],a['Z']];u:=w;end;
end;
writeln(f2,u,' ',max);
close(f1);
close(f2);
end.