城市问题

城市问题

Time Limit:10000MS  Memory Limit:65536K
Total Submit:232 Accepted:88 
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

var
 a:array[0..100,0..100]of longint;
 low:array[0..100]of longint;
 f:array[0..100]of boolean;
 i,j,n,m,s,min,num,t,x,y:longint;
 ans:int64;
begin
 fillchar(f,sizeof(f),true);
 readln(n,s);
 inc(s);
 f[s]:=false;
 for i:=1 to n do
  for j:=1 to n do
  begin
   read(a[i,j]);
   if a[i,j]=-1 then a[i,j]:=maxlongint;
  end;
 for i:=1 to n do
  low[i]:=a[s,i];
 for i:=1 to n do
 begin
  min:=maxlongint;
  for j:=1 to n do//找最短距离
   if (f[j])and(low[j]<min) then
    begin
     num:=j;
     min:=low[j];
    end;
  if num<>0 then
  begin
   f[num]:=false;
   for j:=1 to n do//更新距离
    if (f[j])and(a[num,j]<>maxlongint)and(low[j]>a[num,j]+low[num]) then
    low[j]:=low[num]+a[num,j];
  end;
 end;
 for i:=1 to n do
  write(low[i],' ');
end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值