城市问题
Time Limit:10000MS Memory Limit:65536K
Total Submit:252 Accepted:94
Case Time Limit:1000MS
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
用floyd求追短路
var
map:array[0..101,0..101]of longint;
i,j,n,ans,t:longint;
begin
read(n,ans);
for i:=1 to n do
begin
for j:=1 to n do
begin
read(map[i,j]);
if map[i,j]=-1 then map[i,j]:=maxint;
end;
readln;
end;
for t:=1 to n do
for i:=1 to n do
for j:=1 to n do
begin
if map[i,t]+map[t,j]<map[i,j]
then map[i,j]:=map[i,t]+map[t,j];
end;
for i:=1 to n do write(map[ans+1,i],' ');//ans一定要加一,因为题目是从零开始的
end.