0逝者如斯夫0 @ 2017-08-11 16:33:28
program P3509;
var p,q,i,j,t,last,k,h,n:longint;
go,back:array[0..1900001,1..4] of longint;
g1,g2,dui:array[0..1900001] of longint;
dist:array[0..1900001] of int64;
max:int64;
begin
readln(n);
for t:=1 to n do
begin
readln(p,q);
fillchar(go,sizeof(go),0);
fillchar(back,sizeof(back),0);
fillchar(g1,sizeof(g1),0);
fillchar(g2,sizeof(g2),0);
for i:=1 to q do
begin
readln(go[i,1],go[i,2],go[i,3]);
go[i,4]:=g1[go[i,1]];
g1[go[i,1]]:=i;
back[i,1]:=go[i,2];
back[i,2]:=go[i,1];
back[i,3]:=go[i,3];
back[i,4]:=g2[back[i,1]];
g2[back[i,1]]:=i;
end;
max:=0;
for i:=1 to p do
dist[i]:=maxlongint div 3;
dist[1]:=0;
dui[1]:=1;
i:=1;j:=1;
while i<=j do
begin
last:=j;
for k:=i to last do
begin
h:=g1[dui[k]];
while h<>0 do
begin
if dist[dui[k]]+go[h,3]<dist[go[h,2]]
then begin
dist[go[h,2]]:=dist[dui[k]]+go[h,3];
inc(j);
dui[j]:=go[h,2];
end;
h:=go[h,4];
end;
end;
i:=last+1;
end;
for i:=2 to p do
begin
max:=max+dist[i];
//writeln(1,'to',i,'=',dist[i]);
end;
for i:=1 to p do
dist[i]:=maxlongint div 3;
dist[1]:=0;
dui[1]:=1;
i:=1;j:=1;
while i<=j do
begin
last:=j;
for k:=i to last do
begin
h:=g2[dui[k]];
while h<>0 do
begin
if dist[dui[k]]+back[h,3]<dist[back[h,2]]
then begin
dist[back[h,2]]:=dist[dui[k]]+back[h,3];
inc(j);
dui[j]:=back[h,2];
end;
h:=back[h,4];
end;
end;
i:=last+1;
end;
for i:=2 to p do
begin
max:=max+dist[i];
end;
writeln(max);
end;
end.
赤裸裸的spfa啊,是有什么地方没做好吗?
by 0逝者如斯夫0 @ 2017-08-11 16:35:28
外面n的循环,不关事手动删除