第一问确实不会解,于是我用了个裸的SPFA,直接求的第二问,第二问完全可以用一个简单的最短路求,一遍AC~
program minaw;
const
maxn=100;
maxm=20000;
type
atp=record
y,dis,next:longint;
end;
var
n,m,head,tail:longint;
first,v:array [0..maxn] of longint;
b:array [0..maxn] of boolean;
map:array [0..maxm] of atp;
que:array [0..maxm] of longint;
procedure init;
var
i,x,y,dis:longint;
begin
fillchar(v,sizeof(v),100);
v[1]:=0;
readln(n,m);
for i:=1 to m do
begin
readln(x,y,dis);
map[i].y:=y;
map[i].dis:=dis;
map[i].next:=first[x];
first[x]:=i;
end;
end;
procedure SPFA;
var
t:longint;
begin
fillchar(que,sizeof(que),0);
fillchar(b,sizeof(b),false);
head:=0;
tail:=1;
que[1]:=1;
b[1]:=true;
while head<tail do
begin
inc(head);
t:=first[que[head]];
while t>0 do
begin
if v[que[head]]+map[t].dis<v[map[t].y] then
begin
v[map[t].y]:=v[que[head]]+map[t].dis;
if not b[map[t].y] then
begin
inc(tail);
que[tail]:=map[t].y;
b[map[t].y]:=true;
end;
end;
t:=map[t].next;
end;
b[que[head]]:=false;
end;
end;
begin
assign(input,'minaw.in'); reset(input);
assign(output,'minaw.out'); rewrite(output);
init;
SPFA;
writeln(abs(v[n]));
close(input); close(output);
end.
本文讨论了如何使用裸SPFA算法直接求解最短路径问题,并通过实例展示了其应用,最后提供了完整的代码实现。
1859

被折叠的 条评论
为什么被折叠?



