Stop the Hollyweb! No DRM in HTML5.

POJ 3255 dijkstra次短路

不说什么了,看代码吧。

program p3255;

Type
 rec=record
   e,w,next:longint;
 end;

Var
 n,m,i,j,k,nowmin,minj,mink,u,top,st,ed,ww:longint;
 y:rec;
 a:array[0..200002] of rec;
 b:array[0..10002] of longint;
 v:array[1..2,0..10002] of boolean;
 d:array[1..2,0..10002] of longint;

Procedure add(st,ed,ww:longint);inline;
  begin
  inc(top);
  with a[top] do
    begin
    e:=ed;
    w:=ww;
    next:=b[st];
  end;
  b[st]:=top;
end;

  begin
  readln(n,m);
  top:=0;
  for i:=1 to m do
    begin
    readln(st,ed,ww);
    add(st,ed,ww);
    add(ed,st,ww);
  end;
  fillchar(v,sizeof(v),false);
  fillchar(d,sizeof(d),$7f);
  d[1,1]:=0;
  for i:=1 to 2*n do
    begin
    nowmin:=maxlongint;
    for j:=1 to n do
      for k:=1 to 2 do
        if not v[k,j] then
          if d[k,j]<nowmin then
            begin
            nowmin:=d[k,j];
            minj:=j;mink:=k;
          end;

    if nowmin=maxlongint then break;
    v[mink,minj]:=true;
    u:=b[minj];
    while u<>0 do
      begin
      y:=a[u];
      u:=y.next;
      if nowmin+y.w<d[1,y.e] then
        begin
        d[2,y.e]:=d[1,y.e];
        d[1,y.e]:=nowmin+y.w;
      end else
      if nowmin+y.w<d[2,y.e] then
        d[2,y.e]:=nowmin+y.w;
    end;
  end;
  writeln(d[2,n]);
  readln;
end.

 

posted on 2013-05-16 19:46  灰天飞雁  阅读(210)  评论(0编辑  收藏  举报

填写您的邮件地址,订阅我们的精彩内容:  点击这里给我发消息

添加到收藏夹