Pascal classic algorithm details - Utopian transportation center

From , 2 Years ago, written in Delphi (Object Pascal), viewed 209 times.
URL https://pastebin.vip/view/8606f35e
  1. {思路:利用FLOYD算法求出所有结点的最短路径矩阵,
  2. 然后求出每个结点到其他的结点的距离总合,取最小的那个}
  3. program capital;
  4. const
  5.   maxn=100;
  6. var
  7.   n,m,k,i,j:integer;
  8.   min,sum:longint;
  9.   dist:array[1..maxn,1..maxn] of longint;
  10.   {prev:array[1..maxn,1..maxn] of 0..maxn;}  {因为无需知道路径,因此略去计算前驱的数组}
  11. procedure init;
  12.   var
  13.     m,i,u,v:integer;
  14.   begin
  15.     assign(input,'capital.in');
  16.     reset(input);
  17.     assign(output,'capital.out');
  18.     rewrite(output);
  19.     readln(n,m);
  20.     {fillchar(prev,sizeof(prev),0);}
  21.     for u:=1 to n do
  22.        for v:=1 to n do
  23.           dist[u,v]:=1000000000;
  24.     for i:=1 to m do
  25.       begin
  26.         readln(u,v,dist[u,v]);
  27.         dist[v,u]:=dist[u,v];
  28.         {prev[u,v]:=u;
  29.         prev[v,u]:=v;}
  30.       end;
  31.     {readln(s,t);}
  32.   end;
  33. procedure floyd;
  34.   var
  35.     i,j,k:integer;
  36.   begin
  37.     for k:=1 to n do
  38.       for i:=1 to n do
  39.         for j:=1 to n do
  40.            if (dist[i,k]+dist[k,j]<dist[i,j]) then
  41.               begin
  42.                  dist[i,j]:=dist[i,k]+dist[k,j];
  43.                  {prev[i,j]:=prev[k,j];}
  44.               end;
  45.   end;{floyd}
  46. {procedure print(i,j:integer);   打印路径过程也不需要
  47.   begin
  48.     if i=j
  49.       then write(i)
  50.       else if prev[i,j]=0
  51.            then write('No Solution!')
  52.            else begin
  53.                   print(i,prev[i,j]);
  54.                   write('->',j);
  55.                 end;
  56.   end;}
  57. begin
  58.   init;
  59.   floyd;
  60.   min:=100000000;
  61.   for i:=1 to n do
  62.    begin
  63.      sum:=0;
  64.      for j:=1 to n do
  65.         if i<>j                       {自己到自己的路径不能计算在内}
  66.           then sum:=sum+dist[i,j];
  67.      if min>sum
  68.         then begin
  69.               min:=sum;
  70.               k:=i;
  71.              end;
  72.    end;
  73.   write(k);
  74.   close(input);
  75.   close(output);
  76. end.
  77. //delphi/7194

Reply to "Pascal classic algorithm details - Utopian transportation center"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website