?? 最短路徑(單源dijkstra+binary_heap鄰接表形式).txt
字號(hào):
//單源最短路徑,dijkstra算法+二分堆,鄰接表形式,復(fù)雜度O(mlogm)
//求出源s到所有點(diǎn)的最短路經(jīng),傳入圖的大小n和鄰接表list
//返回到各點(diǎn)最短距離min[]和路徑pre[],pre[i]記錄s到i路徑上i的父結(jié)點(diǎn),pre[s]=-1
//可更改路權(quán)類型,但必須非負(fù)!
#define MAXN 200
#define inf 1000000000
typedef int elem_t;
struct edge_t{
int from,to;
elem_t len;
edge_t* next;
};
#define _cp(a,b) ((a).d<(b).d)
struct heap_t{elem_t d;int v;};
struct heap{
heap_t h[MAXN*MAXN];
int n,p,c;
void init(){n=0;}
void ins(heap_t e){
for (p=++n;p>1&&_cp(e,h[p>>1]);h[p]=h[p>>1],p>>=1);
h[p]=e;
}
int del(heap_t& e){
if (!n) return 0;
for (e=h[p=1],c=2;c<n&&_cp(h[c+=(c<n-1&&_cp(h[c+1],h[c]))],h[n]);h[p]=h[c],p=c,c<<=1);
h[p]=h[n--];return 1;
}
};
void dijkstra(int n,edge_t* list[],int s,elem_t* min,int* pre){
heap h;
edge_t* t;heap_t e;
int v[MAXN],i;
for (i=0;i<n;i++)
min[i]=inf,v[i]=0,pre[i]=-1;
h.init();min[e.v=s]=e.d=0,h.ins(e);
while (h.del(e))
if (!v[e.v])
for (v[e.v]=1,t=list[e.v];t;t=t->next)
if (!v[t->to]&&min[t->from]+t->len<min[t->to])
pre[t->to]=t->from,min[e.v=t->to]=e.d=min[t->from]+t->len,h.ins(e);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -