我们首先来看一下什么是前向星.

 

前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序,

并记录下以某个点为起点的所有边在数组中的起始位置和存储长度,那么前向星就构造好了.

 

用len[i]来记录所有以i为起点的边在数组中的存储长度.

用head[i]记录以i为边集在数组中的第一个存储位置.

 

那么对于下图:

 


 

 

我们输入边的顺序为:

 

1 2

2 3

3 4

1 3

4 1

1 5

4 5

 

那么排完序后就得到:

 

编号:     1      2      3      4      5      6      7

起点u:    1      1      1      2      3      4      4

终点v:    2      3      5      3      4      1      5

 

得到:

 

head[1] = 1    len[1] = 3

head[2] = 4    len[2] = 1

head[3] = 5    len[3] = 1

head[4] = 6    len[4] = 2

 

但是利用前向星会有排序操作,如果用快排时间至少为O(nlog(n))

 

 

如果用链式前向星,就可以避免排序.

 

我们建立边结构体为:

 

struct Edge
{
int next;
int to;
int w;
};

 

其中edge[i].to表示第i条边的终点,edge[i].next表示与第i条边同起点的下一条边的存储位置,edge[i].w为边权值.

 

另外还有一个数组head[],它是用来表示以i为起点的第一条边存储的位置,实际上你会发现这里的第一条边存储的位置其实

在以i为起点的所有边的最后输入的那个编号.

 

head[]数组一般初始化为-1,对于加边的add函数是这样的:

void add(int u,int v,int w)
{
edge[cnt].w = w;
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}

 初始化cnt = 0,这样,现在我们还是按照上面的图和输入来模拟一下:

 

edge[0].to = 2;     edge[0].next = -1;      head[1] = 0;

edge[1].to = 3;     edge[1].next = -1;      head[2] = 1;

edge[2].to = 4;     edge[2],next = -1;      head[3] = 2;

edge[3].to = 3;     edge[3].next = 0;       head[1] = 3;

edge[4].to = 1;     edge[4].next = -1;      head[4] = 4;

edge[5].to = 5;     edge[5].next = 3;       head[1] = 5;

edge[6].to = 5;     edge[6].next = 4;       head[4] = 6;

 

很明显,head[i]保存的是以i为起点的所有边中编号最大的那个,而把这个当作顶点i的第一条起始边的位置.

 

这样在遍历时是倒着遍历的,也就是说与输入顺序是相反的,不过这样不影响结果的正确性.

比如以上图为例,以节点1为起点的边有3条,它们的编号分别是0,3,5   而head[1] = 5

 

我们在遍历以u节点为起始位置的所有边的时候是这样的:

 

for(int i=head[u];~i;i=edge[i].next)

 

那么就是说先遍历编号为5的边,也就是head[1],然后就是edge[5].next,也就是编号3的边,然后继续edge[3].next,也

就是编号0的边,可以看出是逆序的.

模板代码1:

 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <queue>
5 using namespace std;
6 const int inf = 0x3f3f3f3f;
7 const int M = 4444;
8 int d[M],head[M],vis[M];
9 struct nod{
10 int nex,to,w;
11 }eg[M];
12 typedef pair<int,int> P;
13 int cnt=0;
14 inline void add(int u,int v,int w){
15 eg[cnt].to=v;
16 eg[cnt].w=w;
17 eg[cnt].nex=head[u];
18 head[u]=cnt++;
19 }
20 void dijkstra(int s){
21 priority_queue<P,vector<P>,greater<P> >que;
22
23 d[s]=0;
24 que.push(P(0,s));
25 while(!que.empty()){
26 P p = que.top();
27 que.pop();
28 int v=p.second;
29 if(d[v]<p.first) continue;
30 for(int i=head[v];~i;i=eg[i].nex){
31 nod e=eg[i];
32 if(e.w+d[v]<d[e.to]){
33 d[e.to]=e.w+d[v];
34 que.push(P(d[e.to],e.to));
35 }
36 }
37 }
38 }
39 int main(){
40 int t,n;
41 scanf("%d %d",&t,&n);
42 memset(d,inf,sizeof(d));
43 memset(head,-1,sizeof(head));
44 for(int i=0;i<t;i++){
45 int u,v,cost;
46 scanf("%d %d %d",&u,&v,&cost);
47 add(u,v,cost);
48 add(v,u,cost);
49 }
50 dijkstra(1);
51 printf("%d\n",d[n]);
52 return 0;
53 }

 转自:http://blog.csdn.net/acdreamers/article/details/16902023

      http://blog.csdn.net/henuwhr/article/details/76668590

      

p.p1 { margin: 0; font: 18px Menlo; color: rgba(201, 27, 19, 1) }
p.p2 { margin: 0; font: 18px Menlo; color: rgba(130, 46, 14, 1) }
p.p3 { margin: 0; font: 18px Menlo; color: rgba(195, 34, 117, 1) }
p.p4 { margin: 0; font: 18px Menlo }
p.p5 { margin: 0; font: 18px Menlo; color: rgba(4, 53, 255, 1) }
p.p6 { margin: 0; font: 18px Menlo; color: rgba(97, 34, 174, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures; color: rgba(130, 46, 14, 1) }
span.s2 { font-variant-ligatures: no-common-ligatures }
span.s3 { font-variant-ligatures: no-common-ligatures; color: rgba(201, 27, 19, 1) }
span.s4 { font-variant-ligatures: no-common-ligatures; color: rgba(4, 53, 255, 1) }
span.s5 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 0, 0, 1) }
span.s6 { font-variant-ligatures: no-common-ligatures; color: rgba(112, 61, 170, 1) }
span.s7 { font-variant-ligatures: no-common-ligatures; color: rgba(97, 34, 174, 1) }
span.s8 { font-variant-ligatures: no-common-ligatures; color: rgba(195, 34, 117, 1) }
span.s9 { font-variant-ligatures: no-common-ligatures; color: rgba(83, 154, 164, 1) }
span.s10 { font-variant-ligatures: no-common-ligatures; color: rgba(61, 29, 129, 1) }
p.p1 { margin: 0; font: 18px Menlo; color: rgba(201, 27, 19, 1) }
p.p2 { margin: 0; font: 18px Menlo; color: rgba(130, 46, 14, 1) }
p.p3 { margin: 0; font: 18px Menlo; color: rgba(195, 34, 117, 1) }
p.p4 { margin: 0; font: 18px Menlo }
p.p5 { margin: 0; font: 18px Menlo; color: rgba(4, 53, 255, 1) }
p.p6 { margin: 0; font: 18px Menlo; color: rgba(97, 34, 174, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures; color: rgba(130, 46, 14, 1) }
span.s2 { font-variant-ligatures: no-common-ligatures }
span.s3 { font-variant-ligatures: no-common-ligatures; color: rgba(201, 27, 19, 1) }
span.s4 { font-variant-ligatures: no-common-ligatures; color: rgba(4, 53, 255, 1) }
span.s5 { font-variant-ligatures: no-common-ligatures; color: rgba(0, 0, 0, 1) }
span.s6 { font-variant-ligatures: no-common-ligatures; color: rgba(112, 61, 170, 1) }
span.s7 { font-variant-ligatures: no-common-ligatures; color: rgba(97, 34, 174, 1) }
span.s8 { font-variant-ligatures: no-common-ligatures; color: rgba(195, 34, 117, 1) }
span.s9 { font-variant-ligatures: no-common-ligatures; color: rgba(83, 154, 164, 1) }
span.s10 { font-variant-ligatures: no-common-ligatures; color: rgba(61, 29, 129, 1) }

模板 Dijkstra+链式前向星+堆优化(非原创)的更多相关文章

  1. 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)

    Dijkstra+ 链式前向星+ 优先队列   Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...

  2. HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】

    最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...

  3. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

  4. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  5. 洛谷P3371单源最短路径Dijkstra版(链式前向星处理)

    首先讲解一下链式前向星是什么.简单的来说就是用一个数组(用结构体来表示多个量)来存一张图,每一条边的出结点的编号都指向这条边同一出结点的另一个编号(怎么这么的绕) 如下面的程序就是存链式前向星.(不用 ...

  6. Floyd && Dijkstra +邻接表 +链式前向星(真题讲解来源:城市路)

    1381:城市路(Dijkstra) 时间限制: 1000 ms         内存限制: 65536 KB提交数: 4066     通过数: 1163 [题目描述] 罗老师被邀请参加一个舞会,是 ...

  7. 三种邻接表存图模板:vector邻接表、数组邻接表、链式前向星

    vector邻接表: ; struct Edge{ int u,v,w; Edge(int _u=0,int _v=0,int _w=0){u=_u,v=_v,w=_w;} }; vector< ...

  8. 网络流dinic模板,邻接矩阵+链式前向星

    //这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...

  9. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

随机推荐

  1. 使用CDN访问免备案网站

    如何使用CDN绕过服务器域名备案 前言 不得不说,大陆需要备案,时间真的有点长,至少得5天~20天起步,对于我们这些火急火燎的站长还是比较难受的.这里教大家如何使用cdn绕过备案, 访问速度很快,亲测 ...

  2. Golang应用性能问题排查分析

    背景 公司有一个使用golang开发的采集模块,负责调用多个外部系统采集数据:最近做了一次架构上的调整,将采集模块分成api.job两个子模块,并部署到容器中,拆分前部署在虚机上. 现象 部分采集任务 ...

  3. linux设备

    设备初始化时同样要执行一个device_register函数,该函数传入一个struct device *类型的指针,因此要定义一个struct device类型的变量作为我们的设备. struct ...

  4. JVM重新认识(一)oop-klass模型--HSDB使用验证

    一:oop-kclass模型 思考:我们平时写的java类编译成.class文件,JVM加载.class文件,那么加载.class文件之后在JVM中就是oop-kclass(C++)模型形式存在的. ...

  5. 在QML 中用javascritpt 将中文转换拼音,可以在音标

    项目需要, 今天整理了一下.在QML调用javascrit将中文汉字转换成拼音. 感觉执行效率低.下面是主要代码. 具体代码请参考QMLPinyin 代码 ```import "./piny ...

  6. 1、剑指offer-数组——二维数组中的查找

    *题目描述* **在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含 ...

  7. int ping = 11; 限流 客户端验证与服务端是连接的

    int ping = 11; ZooKeeper Programmer's Guide https://zookeeper.apache.org/doc/r3.1.2/zookeeperProgram ...

  8. RabbitMQ入门看这一篇就够了

    一文搞懂 RabbitMQ 的重要概念以及安装 一 RabbitMQ 介绍 这部分参考了 <RabbitMQ实战指南>这本书的第 1 章和第 2 章. 1.1 RabbitMQ 简介 Ra ...

  9. 慕课网金职位 Python工程师 百度网盘下载

    百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...

  10. Java——定时任务调度工具

    一.什么是定时任务调度? 1.常用的定时调度工具:Timer和Quartz 二.Timer简介 1.Timer的定义以及架构 2.Timer示例 三.Timer的定时调度函数 1.schedule的四 ...