城市问题-SSL 1761

来源:互联网 发布:取名软件推荐 编辑:程序博客网 时间:2024/05/17 02:06
Description   设有n个城市,依次编号为012,……,n-1(n<=100),另外有一个文件保存n个城市之间的距离(每座城市之间的距离都小于等于1000)。当两城市之间的距离等于-1时,表示这两个城市没有直接连接。求指定城市k到每一个城市i(0<=I,k<=n-1)的最短距离。 Input 第一行有两个整数n和k,中间用空格隔开;以下是一个NxN的矩阵,表示城市间的距离,数据间用空格隔开。Output 输出指定城市k到各城市间的距离(从第0座城市开始,中间用空格分开)Sample Input 3 10 3 13 0 21 2 0Sample Output 3 0 2floyed:const  maxn=1000;var  a:array[0..maxn,0..maxn] of longint;  n,i,j,k,m:longint;begin  readln(n,m);  inc(m);  for i:=1 to n do   begin    for j:=1 to n do     begin       read(a[i,j]);       if a[i,j]=-1 then a[i,j]:=maxlongint;     end;     readln;   end;   for k:=1 to n do   for i:=1 to n do    for j:=1 to n do    if (i<>j) and (j<>k) and (i<>k) then     if (a[i,k]+a[k,j]<a[i,j]) then      if (a[i,k]<>maxlongint) and (a[k,j]<>maxlongint) then       a[i,j]:=a[i,k]+a[k,j];       for i:=1 to n-1 do       write(a[m,i],' ');       write(a[m,n]);end.
0 0
原创粉丝点击