题目大意:求无向图的次短路。

分析:

在起点终点各求一次最短路,枚举边,通过该边的最短路为其权值加上到起点和终点最短路之和,找到最短但又比最短路长的路径。

代码:

program block;
type
point=^node;
node=record
v,c:longint; next:point; end;
var
a:array[..]of point;
dis:array[..,..]of longint;
q:array[..]of longint;
g:array[..]of boolean;
e:array[..,..]of longint;
n,i,m,x,y,v,ans:longint; p:point;
procedure add(x,y,c:longint);
var p:point;
begin
new(p); p^.v:=y; p^.c:=c; p^.next:=a[x]; a[x]:=p;
end;
procedure spfa(s,d,l:longint);
var i,h,t,u,v,k:longint;
begin
fillchar(g,sizeof(g),false);
for i:= to n do dis[i,l]:=maxlongint div ; dis[s,l]:=; g[s]:=true;
h:=; t:=; q[]:=s;
while h<t do
begin
inc(h); u:=q[h]; g[u]:=false; new(p); p:=a[u];
while p<>nil do
begin
v:=p^.v;
if dis[v,l]>dis[u,l]+p^.c then
begin
dis[v,l]:=dis[u,l]+p^.c;
if g[v]=false then begin g[v]:=true; inc(t);q[t]:=v;end;
end;
p:=p^.next;
end;
end;
end;
begin
assign(input,'block.in');
reset(input);
assign(output,'block.out');
rewrite(output);
readln(n,m);
for i:= to m do
begin
readln(x,y,v); e[i,]:=x; e[i,]:=y; e[i,]:=v;
add(x,y,v); add(y,x,v);
end;
spfa(,n,); spfa(n,,); ans:=maxlongint;
for i:= to m do
begin
v:=dis[e[i,],]+dis[e[i,],]+e[i,]; if (v<ans)and(v>dis[n,]) then ans:=v;
v:=dis[e[i,],]+dis[e[i,],]+e[i,]; if (v<ans)and(v>dis[n,]) then ans:=v;
end;
writeln(ans);
close(input); close(output);
end.

最新文章

  1. JQuery 滚动轮播
  2. 开始使用DOJO(翻译)
  3. mha报错
  4. shell脚本变量
  5. cocos布局分析
  6. BZOJ4046 [Cerc2014] Pork barre
  7. Pomelo:网易开源基于 Node.js 的游戏服务端框架
  8. Backbone模型
  9. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]
  10. Hadoop集群(第6期)_WordCount运行详解
  11. Frequent values
  12. VS SQL 出现%CommonDir%dte80a.olb 该解决方案
  13. Python 模块之 string.py
  14. JPA(三)之实体关系一对多(多对一)
  15. Visual Studio 2017 IDE之xml过大报错
  16. kibana升级之后原本保存的数据dashboards, visualizations, index patterns丢失
  17. 1分钟了解MyISAM与InnoDB的索引差异
  18. EasyUI datagrid 一个可以 直接运行例子一个文件 六
  19. 一些减少代码量、提高开发效率的利器(Java)
  20. Bootstrap 基本模板理解

热门文章

  1. SQL 值得记住的点
  2. Mybatis generator(复制粘贴完成)
  3. How to save console output to a file in Eclipse
  4. Oracle 字符串处理函数
  5. 人品计算器 JFrame 窗体软件版 JPanel JTextField JTextArea JButtton JLabel setContentPane Swing包(用户界面工具包)
  6. 第十三篇、OC_UICollectionView的基本配置
  7. 网络流_spfa最小费用最大流
  8. SpingBoot之配置文件的值注入问题
  9. Fibonacci again and again HDU - 1848
  10. HDU1301 Jungle Roads