sine吧 关注:24贴子:175
  • 0回复贴,共1
 var
  i,j,n,min,temp,minj:integer;
  map:array[1..100,1..100] of integer;
  dist:array[1..100] of integer;
  used:array[1..100] of boolean;

 begin
  fillchar(used,sizeof(used),false);
  fillchar(map,sizeof(map),0);
  fillchar(dist,sizeof(dist),0);
  read(n);
  for i:=1 to n do
   for j:=1 to n do
    begin
     read(map[i,j]);
     if map[i,j]=0 then map[i,j]:=maxint;
    end;
  for i:=1 to n do dist[i]:=map[1,i];
  dist[1]:=0;
  used[1]:=true;
  for i:=1 to n-1 do
  begin
   min:=maxint;
   for j:=1 to n do
    if (used[j]=false) and (dist[j]<min) then
                                         begin
                                          min:=dist[j];
                                          minj:=j;
                                         end;
   used[minj]:=true;
  for j:=1 to n do
   if (not used[j]) and (dist[minj]<>maxint) and (map[minj,j]<>maxint) then
                   begin
                    temp:=dist[minj]+map[minj,j];
                    if  temp<dist[j] then dist[j]:=temp;
                   end;
  end;
 writeln(dist[4]);
end.


1楼2006-08-08 22:19回复