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