您的位置:首页 > 其它

JudgeOnline 1636 城市交通!!!

2017-02-25 08:43 169 查看
城市交通
Time Limit:1000MS  Memory Limit:65536K

Total Submit:93 Accepted:83
Description
  有n个城市,编号1~n,有些城市之间有路相连,有些则没有,有路则当然有一个距离。现在规定只能从编号小的城市到编号大的城市,问你从编号为1的城市到编号为n的城市之间的最短距离是多少?

Input

先输入一个n,表示城市数,n小于100。

下面的n行是一个n*n的邻接矩阵map[i,j],其中map[i,j]=0表示城市i和城市j之间没有路相连,否则为两者之间的距离。

Output
输出格式:一个数,表示最少要多少时间。

输入数据保证可以从城市1飞到城市n。

Sample Input

11
0 5 3 0 0 0 0 0 0 0 0
5 0 0 1 6 3 0 0 0 0 0
3 0 0 0 8 0 4 0 0 0 0
0 1 0 0 0 0 0 5 6 0 0
0 6 8 0 0 0 0 5 0 0 0
0 3 0 0 0 0 0 0 0 8 0
0 0 4 0 0 0 0 0 0 3 0
0 0 0 5 5 0 0 0 0 0 3
0 0 0 6 0 0 0 0 0 0 4
0 0 0 0 0 8 3 0 0 0 3
0 0 0 0 0 0 0 3 4 3 0


Sample Output

13


Source
elba

公式:

      f[i]:=max(f[j]+a[i,j]);

var
a:array[1..100]of longint;
map:array[1..100,1..100]of longint;
n,i,j,min:longint;
begin
readln(n);
for i:=1 to n do
begin
for j:=1 to n do
read(map[i,j]);
readln;
end;
a[1]:=0;
for i:=2 to n do
begin
min:=maxlongint;
for j:=1 to i-1 do
if map[j,i]<>0  then
if a[j]+map[j,i]<min then min:=a[j]+map[j,i];
a[i]:=min;
end;
writeln(a
);

end.


方法2:

var
a:array[1..100]of longint;
map:array[1..100,1..100]of longint;
n,i,j,min,ans:longint;
begin
readln(n);
for i:=1 to n do
for j:=1 to n do
read(map[i,j]);
for i:=n-1 downto 1 do
begin
min:=maxlongint;
for j:=i+1 to n do
if map[i,j]<>0  then
if a[j]+map[i,j]<min then min:=a[j]+map[i,j];
a[i]:=min;
end;
writeln(a[1]);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: