level 11
百星高照
楼主
var
n,m,i,a,b,t,head,tail,max,j:longint;
cost,sost:array[1..1000,1..1000] of longint;
dist:array[1..1000] of longint;
v:array[1..1000] of boolean;
q,p:array[1..1000] of longint;
procedure spfa(a,b:longint);
var x:longint;
begin
fillchar(v,sizeof(v),false);fillchar(q,sizeof(q),0);
for i:=1 to n do dist[i]:=maxlongint div 3;
head:=0;tail:=1;
v[a]:=true;q[tail]:=a;dist[a]:=0;
while head<>tail do
begin
head:=(head mod n)+1;
x:=q[head];
for i:=1 to n do
if (cost[x,i]>0) and (cost[x,i]+dist[x]<dist[i]) then
begin
dist[i]:=cost[x,i]+dist[x];
p[i]:=x;
if not(v[i]) then
begin
tail:=(tail mod n)+1;
q[tail]:=i;
v[i]:=true;
end;
end;
end;
if dist[1]>max then max:=dist[1];
end;
procedure spfa2(a,b:longint);
var x:longint;
begin
fillchar(v,sizeof(v),false);fillchar(q,sizeof(q),0);
for i:=1 to n do dist[i]:=maxlongint div 3;
head:=0;tail:=1;
v[a]:=true;q[tail]:=a;dist[a]:=0;
while head<>tail do
begin
head:=(head mod n)+1;
x:=q[head];
for i:=1 to n do
if (cost[x,i]>0) and (cost[x,i]+dist[x]<dist[i]) then
begin
dist[i]:=cost[x,i]+dist[x];
if not(v[i]) then
begin
tail:=(tail mod n)+1;
q[tail]:=i;
v[i]:=true;
end;
end;
end;
if dist[1]>max then max:=dist[1];
end;
begin
readln(n,m);
for i:=1 to m do
begin
readln(a,b,t);
cost[a,b]:=t;
cost[b,a]:=t;
end;
spfa(n,1);
max:=0;
j:=1;
while j<>n do
begin
sost[j,p[j]]:=cost[j,p[j]];
cost[j,p[j]]:=maxlongint div 3;
cost[p[j],j]:=maxlongint div 3;
spfa2(n,1);
cost[j,p[j]]:=sost[j,p[j]];
cost[p[j],j]:=sost[j,p[j]];
j:=p[j];
end;
writeln(max);
end.
2015年07月22日 02点07分
1
n,m,i,a,b,t,head,tail,max,j:longint;
cost,sost:array[1..1000,1..1000] of longint;
dist:array[1..1000] of longint;
v:array[1..1000] of boolean;
q,p:array[1..1000] of longint;
procedure spfa(a,b:longint);
var x:longint;
begin
fillchar(v,sizeof(v),false);fillchar(q,sizeof(q),0);
for i:=1 to n do dist[i]:=maxlongint div 3;
head:=0;tail:=1;
v[a]:=true;q[tail]:=a;dist[a]:=0;
while head<>tail do
begin
head:=(head mod n)+1;
x:=q[head];
for i:=1 to n do
if (cost[x,i]>0) and (cost[x,i]+dist[x]<dist[i]) then
begin
dist[i]:=cost[x,i]+dist[x];
p[i]:=x;
if not(v[i]) then
begin
tail:=(tail mod n)+1;
q[tail]:=i;
v[i]:=true;
end;
end;
end;
if dist[1]>max then max:=dist[1];
end;
procedure spfa2(a,b:longint);
var x:longint;
begin
fillchar(v,sizeof(v),false);fillchar(q,sizeof(q),0);
for i:=1 to n do dist[i]:=maxlongint div 3;
head:=0;tail:=1;
v[a]:=true;q[tail]:=a;dist[a]:=0;
while head<>tail do
begin
head:=(head mod n)+1;
x:=q[head];
for i:=1 to n do
if (cost[x,i]>0) and (cost[x,i]+dist[x]<dist[i]) then
begin
dist[i]:=cost[x,i]+dist[x];
if not(v[i]) then
begin
tail:=(tail mod n)+1;
q[tail]:=i;
v[i]:=true;
end;
end;
end;
if dist[1]>max then max:=dist[1];
end;
begin
readln(n,m);
for i:=1 to m do
begin
readln(a,b,t);
cost[a,b]:=t;
cost[b,a]:=t;
end;
spfa(n,1);
max:=0;
j:=1;
while j<>n do
begin
sost[j,p[j]]:=cost[j,p[j]];
cost[j,p[j]]:=maxlongint div 3;
cost[p[j],j]:=maxlongint div 3;
spfa2(n,1);
cost[j,p[j]]:=sost[j,p[j]];
cost[p[j],j]:=sost[j,p[j]];
j:=p[j];
end;
writeln(max);
end.




