Bootstrap

java 最短路径 邻接表_最短路径的Dijkstra算法(邻接表)

描述

以邻接表作为存储结构实现,求解从给定源点到给定结束点的最短路径。

输入

从1开始表示第一个节点。

第一行输入:顶点数n(2<=n<=100),边数m(2<=m<=100)

第二行输入有向边:起始点s1,结束点 s2,边权值 w

第三行输入:源点start,终点end

输出

若存在路径,输出路径长度;

若不存在,输出-1。

输入样例

6 8

1 6 100

1 5 30

1 3 10

2 3 5

3 4 50

4 6 10

5 4 20

5 6 60

1 6

输出样例

60

#include

#include

#include

#include

#define maxnum 120

#define INF 10000000

using namespace std;

typedef char VertexType;

//边

typedef struct ArcNode

{

int adjvex;

int weight;

struct ArcNode *nextarc;

}ArcNode;

//顶点

typedef struct VNode

{

VertexType data;

ArcNode *firstarc;

}VNode, AdjList[maxnum];

typedef struct

{

AdjList vertices;//数组

int vexnum, arcnum;

}ALGraph;

//顶点节点,保存id和到源顶点的估算距离,优先队列需要的类型

struct Node

{

int id;//源顶点id

int w;//估算距离

//因要实现最小堆,按升序排列,因而需要重载运算符,重定义优先级,以小为先

friend bool operator < (struct Node a, struct Node b)

{

return a.w > b.w;

}

};

int path[maxnum];

int visited[maxnum] = {0};

Node dist[maxnum];

priority_queueq;

void Dijkstra(ALGraph g, int v0, int n)

{

//初始化

for(int i = 1; i <= n; i++)

{

dist[i].id = i;

dist[i].w = INF;

path[i] = -1; //每个顶点都无父亲节点

visited[i] = 0; //都未找到最短路

}

dist[v0].w = 0;

q.push(dist[v0]);

while(!q.empty())

{

Node cd = q.top();

q.pop();

int u = cd.id;

if(visited[u])

continue;

visited[u] = 1;

ArcNode *p = g.vertices[u].firstarc;

while(p)

{

int tempv = p->adjvex;

int tempw = p->weight;

if(!visited[tempv] && dist[tempv].w > dist[u].w+tempw)

{

dist[tempv].w = dist[u].w+tempw;

path[tempv] = u;

q.push(dist[tempv]);

}

p = p->nextarc;

}

}

}

void CreateALGraph(ALGraph &g, int arc, int vex)

{

g.arcnum = arc;

g.vexnum = vex;

int v1, v2, i, w;

for(i = 1; i <= vex; i++)

{

g.vertices[i].firstarc = NULL;

}

for(i = 1; i <= arc; i++)

{

cin >> v1 >> v2 >> w;

ArcNode *q = (ArcNode*)malloc(sizeof(ArcNode));

q->adjvex = v2;

q->weight = w;

q->nextarc = g.vertices[v1].firstarc;

g.vertices[v1].firstarc = q;

}

}

int DFS(ALGraph g, int i, int j)

{

visited[i] = 1;

ArcNode *p = g.vertices[i].firstarc;

while(p)

{

if(p->adjvex == j)

return 1;

//cout <adjvex])<< endl;

if(!(visited[p->adjvex]) && DFS(g, p->adjvex, j))

return 1;

p = p->nextarc;

}

return 0;

}

int BFS(ALGraph g, int i, int j)

{

queueq;//

q.push(i);

visited[i] = 1;

ArcNode *p;

while(!q.empty())

{

int temp = q.front();

q.pop();

p = g.vertices[temp].firstarc;

while(p)

{

//cout << p->adjvex;

if(p->adjvex == j)

return 1;

if(!(visited[p->adjvex]))

{

visited[p->adjvex] = 1;

q.push(p->adjvex);

}

p = p->nextarc;

}

}

return 0;//返回不可少

}

int main()

{

int m, n;

//顶点,边

cin >> n >> m;

ALGraph g;

CreateALGraph(g, m, n);

// for(int i = 1; i <= n; i++)

// {

// ArcNode *p = g.vertices[i].firstarc;

// cout << "i = " << i << ": ";

// while(p)

// {

// cout << p->adjvex;

// p = p->nextarc;

// }

//cout << endl;

// }

int v0, ve;

cin >> v0 >> ve;

Dijkstra(g, v0, n);

if(dist[ve].w != INF)

cout << dist[ve].w << endl;

else

cout << -1 <

return 0;

}

;