堆优化的dijkstra

type re=record
    point,x:longint;
    end;
var n,m,i,j,k,l,x,ii,y,z,tot,top,point,v:longint;
head,next,vet,len,dist:array[0..1000005] of longint;
p:array[0..1000005] of re;
b:array[0..1000005] of boolean;

procedure put(point,len:longint);
var son,fa:longint;
y:re;
begin
inc(top);
p[top].point:=point;
p[top].x:=len;
son:=top;
while son>1 do begin
    if p[son shr 1].x>p[son].x then begin
        y:=p[son];p[son]:=p[son shr 1];p[son shr 1]:=y;
    end else break;
    son:=son shr 1;
    end;
end;

procedure get;
var son,fa:longint;
y:re;
begin
point:=p[1].point;
p[1]:=p[top];
dec(top);
fa:=1;
while fa*2<=top do begin
    if (fa*2+1>top)or(p[fa*2].x<p[fa*2+1].x) then son:=fa*2 else son:=fa*2+1;
    if p[fa].x>p[son].x then begin y:=p[fa];p[fa]:=p[son];p[son]:=y;end else break;
    fa:=son;
    end;
end;

begin
readln(n,m);
for i:=1 to n do dist[i]:=maxlongint shr 1;
for i:=1 to m do begin
    readln(x,y,z);
    inc(tot);
    next[tot]:=head[x];
    vet[tot]:=y;
    len[tot]:=z;
    head[x]:=tot;
    inc(tot);
    next[tot]:=head[y];
    vet[tot]:=x;
    len[tot]:=z;
    head[y]:=tot;
    end;
top:=1;
p[top].point:=1;
p[top].x:=0;
dist[1]:=0;b[1]:=true;
 while top>0 do begin //for i:=1 to n do begin
    get;
        if point=n then break;
    ii:=head[point];
    while ii<>0 do
        begin
        v:=vet[ii];
        l:=len[ii];
        if dist[point]+l<dist[v] then begin dist[v]:=dist[point]+l;
        //if not b[v] then begin b[v]:=true;put(v,dist[v]);end;
        put(v,dist[v]);
        end;
        ii:=next[ii];
        end;
    end;
writeln(dist[n]);
end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值