唯思有杰

Jeffrey's area
posts(0) comments(0) trackbacks(0)
  • C++博客
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

常用链接

  • 我的随笔
  • 我的评论
  • 我参与的随笔

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • 动态规划
  • 数值与数论
  • 搜索
  • 图论与网络流

文章分类

  • 动态规划
  • 计算几何(1)
  • 数论与数值
  • 搜索
  • 图论与网络流(1)

文章档案

  • 2010年8月 (2)

搜索

  •  

最新评论

View Post

链表SPFA【RQN341】

type point=^rec;
     rec=record
          next:point;
          endv:integer;
          w:longint;
          end;
var head,adj:array[1..20000]of point;
    p,null:point;
    n:integer;e:longint;start,over:integer;
    open,closed:longint;
    q:array[0..1000000]of integer;
    flag:array[1..20000]of boolean;
    dist:array[1..20000]of longint;

procedure init;
var i,stv,edv:integer;
    wei:longint;
begin
fillchar(q,sizeof(q),0);
fillchar(flag,sizeof(flag),false);
fillchar(dist,sizeof(dist),127);
readln(n,e);
for i:=1 to n do
  begin
  new(p);
  head[i]:=p;
  adj[i]:=p;
  end;
for i:=1 to e do
  begin
  readln(stv,edv,wei);
  adj[stv]^.endv:=edv;
  adj[stv]^.w:=wei;
  new(p);
  adj[stv]^.next:=p;
  adj[stv]:=p;
  /////symmetry/////
  adj[edv]^.endv:=stv;
  adj[edv]^.w:=wei;
  new(p);
  adj[edv]^.next:=p;
  adj[edv]:=p
  end;
for i:=1 to n do
  adj[i]^.next:=null;
start:=1;over:=n;
end;

procedure spfa(s:longint);
var dady:integer;
begin
flag[s]:=true;q[1]:=s;dist[s]:=0;
open:=1;closed:=1;
while open<=closed do
  begin
  dady:=q[open];
  p:=head[dady];
  while (p<>nil) do
     begin
     if dist[p^.endv] > dist[dady]+p^.w
     then begin
          dist[p^.endv] := dist[dady]+p^.w;
          if not flag[p^.endv]
          then begin
               inc(closed);
               q[closed]:=p^.endv;
               flag[p^.endv]:=true;
               end;
          end;
     p:=p^.next;
     end;

  flag[dady]:=false;
  inc(open);
  end;
end;


begin
init;
null:=nil;
spfa(start);
writeln(dist[over]);
readln;readln;
end.  

posted on 2010-08-05 16:43 陈斯杰 阅读(132) 评论(0)  编辑 收藏 引用 所属分类: 图论与网络流


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


 
Powered by:
C++博客
Copyright © 陈斯杰