zoukankan      html  css  js  c++  java
  • 城市问题(Floyd)

    Description

      设有n个城市,依次编号为0,1,2,……,n-1(n<=100),另外有一个文件保存n个城市之间的距离(每座城市之间的距离都小于等于1000)。当两城市之间的距离等于-1时,表示这两个城市没有直接连接。求指定城市k到每一个城市i(0<=I,k<=n-1)的最短距离。

    Input

    第一行有两个整数n和k,中间用空格隔开;以下是一个NxN的矩阵,表示城市间的距离,数据间用空格隔开。

    Output

    输出指定城市k到各城市间的距离(从第0座城市开始,中间用空格分开)

    Sample Input

    3 1
    0 3 1
    3 0 2
    1 2 0
    

    Sample Output

    3 0 2





    分析

    输入时,如果a[i,j]=-1 那么就a[i,j]=maxlongint

    用Floyd算法,算出每个点之间的最短距离

    再一个循环输出



    程序如下:

    • var
      n,s,i,j,k:longint;
      a:array[-1..200,-1..200]of longint;
      begin
          readln(n,s);
          for i:=0 to n-1 do
          for j:=0 to n-1 do
          begin
              read(a[i,j]);
              if a[i,j]=-1 then a[i,j]:=maxlongint;
          end;
          for k:=0 to n-1 do
          for i:=0 to n-1 do
          for j:=0 to n-1 do
          if (a[i,k]+a[k,j]<a[i,j])and(a[i,k]<>maxlongint)and(a[k,j]<>maxlongint) then a[i,j]:=a[i,k]+a[k,j];
          for i:=0 to n-1 do
          write(a[s,i],' ');
      end.
      

  • 相关阅读:
    每日总结2.26
    《梦断代码》阅读笔记三
    每日总结2.25
    每日总结2.24
    每日总结2.23
    每日总结2.22
    每日总结2.19
    《梦断代码》阅读笔记二
    Java-11 形参和实参
    Java-10 final用法
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9500144.html
Copyright © 2011-2022 走看看