Dijkstra堆優化 邊表儲存

2021-05-27 14:39:55 字數 1491 閱讀 6712

演算法:dij+heap

步驟:建乙個小根堆,把權值都放進去,每次都在堆裡面取出堆首,然後不斷的維護這個堆,時間複雜度是o(nlogn),比之前o(n²)的演算法快了不少。對於大資料時採用邊表或前向星類似的資料結構存,能優化不少。

program ndk1068;

const

maxn=10000;

maxm=1000000;

type

atp=record

y,next,dis:longint;

end;

agp=record

x,data:longint;

end;

var map:array [0..maxm] of atp;

v:array [0..maxn] of agp;

first:array [0..maxn] of longint;

linkv:array [0..maxn] of longint;

n,m,tot,st,ed:longint;

procedure init;

var i,x,y,dis:longint;

temp:agp;

begin

fillchar(first,sizeof(first),0);

fillchar(map,sizeof(map),0);

readln(n,m);

for i:=1 to m do

begin

readln(x,y,dis);

inc(tot);

map[tot].y:=y;

map[tot].dis:=dis;

map[tot].next:=first[x];

first[x]:=tot;

end;

for i:=1 to n do linkv[i]:=i;

for i:=1 to n do

begin

v[i].x:=i;

v[i].data:=maxlongint;

end;

readln(st,ed);

temp:=v[st];

v[st]:=v[1];

v[1]:=temp;

v[1].data:=0;

end;

procedure ad_up(x:longint);

var temp:agp;

begin

while x shr 1>=1 do

begin

if v[x].datamaxlongint then

begin

while t<>0 do

begin

if (map[t].dis+minmaxlongint then writeln(v[linkv[ed]].data) else writeln('no solution!');

close(input); close(output);

end.

Dijkstra 堆優化 鄰接表

在pat試題中涉及到10000個資料左右的最短路問題時,對迪傑斯特拉演算法用最小堆進行優化可以在很快時間內解決。如果不採用迪傑斯特拉演算法和鄰接表表示則很有可能超時。下面展示了用c 中stl的優先順序佇列對迪傑斯特拉演算法的優化,有了它加上dfs就可以基本解決在pat中遇到的最短路問題 大部分最短路...

dijkstra和dijkstra堆優化模板

之前qaq一直沒有準備堆優化模板,本例以pat a1003為例,整理dijkstra和dijkstra堆優化模板 我們可以發現該篇幅找最小值部分是使用量乙個for迴圈 include include using namespace std int n,m,c1,c2 int edge 510 510...

dijkstra 堆優化 路徑

別人給的模板,所以不知道鏈結 include include include include include using namespace std define maxn 1020 define inf 0x3f typedef long long ll o nlogn typedef pair ...