众所周知,GF同学喜欢打dota,而且打得非常好。今天GF和Spartan同学进行了一场大战。现在GF拿到一张地图,地图上一共有n个地点,GF的英雄处于1号点,Spartan的基地位于n号点,GF要尽快地选择较短的路线让他的英雄去虐掉Spartan的基地。但是Spartan早就料到了这一点,他有可能会开挂(BS~)使用一种特别的魔法,一旦GF所走的路线的总长度等于最短路的总长度时,GF的英雄就要和这种魔法纠缠不休。这时GF就不得不选择非最短的路线。现在请你替GF进行规划。
对于描述的解释与提醒:
1.无向路径,花费时间当然为非负值。
2.对于本题中非最短路线的定义:不管采取任何迂回、改道方式,只要GF所走的路线总长度不等于1到n最短路的总长度时,就算做一条非最短的路线。
3.保证1~n有路可走。
输入格式:
第一行为n,m(表示一共有m条路径)
接下来m行,每行3个整数a,b,c,表示编号为a,b的点之间连着一条花费时间为c的无向路径。
接下来一行有一个整数p,p=0表示Spartan没有开挂使用这种魔法,p=1则表示使用了。
输出格式:
所花费的最短时间t,数据保证一定可以到达n。
样例:
样例输入1:
5 5
1 2 1
1 3 2
3 5 2
2 4 3
4 5 1
0
样例输入2:
5 5
1 2 1
1 3 2
3 5 2
2 4 3
4 5 1
1
样例输出1:
4
样例输出2:
5
数据范围:
对于50%的数据,1<=n,m<=5000
对于70%的数据,1<=n<=10000, 1<=m<=50000,p=0,
对于100%的数据,1<=n<=10000, 1<=m<=50000,p=1
无向图,花费时间c>=0
时间限制:
各个测试点1s
题目来源:
来源:lydliyudong Tyvj February二月月赛第二场 第2道
算法:图论
一道裸的求次短路的题,再次复习了一下求次短路的方法,将所有边都标上号,先以1为起点求最短路,再以n为起点求最短路,然后枚举边,如果v1[i]+map[t].dis+v2[j]>v1[n](t是连接i和j的边)且v1[i]+map[t].dis+v2[j]要保证最小,最小值就是次短路。
program P1450;
const
maxn=10000;
maxm=100000;
type
atp=record
x,y,next,dis:longint;
end;
var
n,m,p,head,tail,tot:longint;
first,v1,v2: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
readln(n,m);
for i:=1 to m do
begin
readln(x,y,dis);
inc(tot);
map[tot].x:=x;
map[tot].y:=y;
map[tot].next:=first[x];
map[tot].dis:=dis;
first[x]:=tot;
inc(tot);
map[tot].y:=x;
map[tot].x:=y;
map[tot].next:=first[y];
map[tot].dis:=dis;
first[y]:=tot;
end;
readln(p);
end;
procedure SPFA;
var
t:longint;
begin
fillchar(b,sizeof(b),false);
fillchar(v1,sizeof(v1),100);
v1[1]:=0;
b[1]:=true;
que[1]:=1;
head:=0;
tail:=1;
while head<tail do
begin
inc(head);
t:=first[que[head]];
while t>0 do
begin
if v1[que[head]]+map[t].dis<v1[map[t].y] then
begin
v1[map[t].y]:=v1[que[head]]+map[t].dis;
if not b[map[t].y] then
begin
inc(tail);
b[map[t].y]:=true;
que[tail]:=map[t].y;
end;
end;
t:=map[t].next;
end;
b[que[head]]:=false;
end;
end;
procedure SPFA1;
var
t:longint;
begin
fillchar(b,sizeof(b),false);
fillchar(v1,sizeof(v1),100);
v1[1]:=0;
b[1]:=true;
que[1]:=1;
head:=0;
tail:=1;
while head<tail do
begin
inc(head);
t:=first[que[head]];
while t>0 do
begin
if v1[que[head]]+map[t].dis<v1[map[t].y] then
begin
v1[map[t].y]:=v1[que[head]]+map[t].dis;
if not b[map[t].y] then
begin
inc(tail);
b[map[t].y]:=true;
que[tail]:=map[t].y;
end;
end;
t:=map[t].next;
end;
b[que[head]]:=false;
end;
end;
procedure SPFA2;
var
t:longint;
begin
fillchar(b,sizeof(b),false);
fillchar(v2,sizeof(v2),100);
v2[n]:=0;
b[1]:=true;
que[1]:=n;
head:=0;
tail:=1;
while head<tail do
begin
inc(head);
t:=first[que[head]];
while t>0 do
begin
if v2[que[head]]+map[t].dis<v2[map[t].y] then
begin
v2[map[t].y]:=v2[que[head]]+map[t].dis;
if not b[map[t].y] then
begin
inc(tail);
b[map[t].y]:=true;
que[tail]:=map[t].y;
end;
end;
t:=map[t].next;
end;
b[que[head]]:=false;
end;
end;
procedure main;
var
i,min:longint;
begin
if p=0 then
begin
SPFA;
writeln(v1[n]);
end;
if p=1 then
begin
SPFA1;
SPFA2;
min:=maxlongint;
for i:=1 to tot do
if (v1[map[i].x]+v2[map[i].y]+map[i].dis>v1[n]) and (v1[map[i].x]+v2[map[i].y]+map[i].dis<min) then min:=v1[map[i].x]+v2[map[i].y]+map[i].dis;
writeln(min);
end;
end;
begin
assign(input,'P1450.in'); reset(input);
assign(output,'P1450.out'); rewrite(output);
init;
main;
close(input); close(output);
end.
本文介绍了一个关于Dota游戏中寻找次短路径的问题及其解决方法。通过使用SPFA算法两次来找到从起点到终点的次短路径,解决了当最短路径不可用时的问题。
176

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



