Dijkstra with Heap

21 篇文章 0 订阅

嗯。。今天在写BZOJ1001的时候顺带写了Dijkstra with Heap

就是在正常的基础上维护一个where数组

代表第i个点在Heap中的位置

然后Heap里存的是点的编号

Code:

procedure sert(a:longint);
var fa:longint;
begin
	if a=1 then exit;
	fa:=a shr 1;
	if d[heap[fa]]>d[heap[a]] then begin
									where[heap[fa]]:=a;
									where[heap[a]]:=fa;
									step:=heap[a];
									heap[a]:=heap[fa];
									heap[fa]:=step;
									sert(fa);
								   end;
end;
procedure down(a:longint);
var son1,son2:longint;
begin
	son1:=a shl 1;
	son2:=a shl 1+1;
	if son1>top then exit;
	if son1=top then
					if d[heap[a]]>d[heap[son1]] then begin
														where[heap[a]]:=son1;
														where[heap[son1]]:=a;
														step:=heap[a];
														heap[a]:=heap[son1];
														heap[son1]:=step;
														exit;
													 end;
	if (d[heap[son1]]<d[heap[son2]]) then
										if d[heap[a]]>d[heap[son1]] then begin
																			where[heap[a]]:=son1;
																			where[heap[son1]]:=a;
																			step:=heap[a];
																			heap[a]:=heap[son1];
																			heap[son1]:=step;
																			down(son1);
																		 end
																	else nothing:=1
									 else
										if d[heap[a]]>d[heap[son2]] then begin
																			where[heap[a]]:=son2;
																			where[heap[son2]]:=a;
																			step:=heap[a];
																			heap[a]:=heap[son2];
																			heap[son2]:=step;
																			down(son2);
																		 end
end;
procedure change(a:longint);
begin
	inc(top);
	where[a]:=top;
	heap[top]:=a;
	sert(top);
end;
procedure Dijkstra;
begin
	for j:=2 to n do
	begin
		x:=heap[1];
		heap[1]:=heap[top];
		where[heap[top]]:=1;
		dec(top);
		down(1);
		i:=headlist[x];
		while i<>-1 do
		begin
			if d[x]+weight[i]<d[t[i]] then if d[t[i]]=maxlongint then begin
 																	d[t[i]]:=d[x]+weight[i];
																		change(t[i]);
																	  end
															     else begin
																		d[t[i]]:=d[x]+weight[i];
																		sert(where[t[i]]);
																	  end;
			i:=next[i];
		end;
	end;
end;
procedure First;
begin
	d[1]:=0;
	for i:=2 to n do d[i]:=maxlongint;
	i:=headlist[1];
	while (i<>-1) do
	begin
		d[t[i]]:=weight[i];
		change(t[i]);
        i:=next[i];
	end;
end;
procedure main;
begin
	init;
	First;
	Dijkstra;
	writeln(d[n]);
end;
begin
	main;
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值