⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 最短路径(单源dijkstra+binary_heap邻接表形式).txt

📁 浙大acm模板。。。。。。。。。。。。。。。。。。。。。。。。。。。
💻 TXT
字号:
//单源最短路径,dijkstra算法+二分堆,邻接表形式,复杂度O(mlogm)
//求出源s到所有点的最短路经,传入图的大小n和邻接表list
//返回到各点最短距离min[]和路径pre[],pre[i]记录s到i路径上i的父结点,pre[s]=-1
//可更改路权类型,但必须非负!
#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);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -