Dijkstra with Heap

嗯。。今天在写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[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]-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.


你可能感兴趣的:(模板,2014)