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

本文介绍了如何使用邻接表作为数据结构来解决有向图中从源点到目标点的最短路径问题。通过Dijkstra算法实现,包括输入输出格式、创建有向图、调用Dijkstra算法并输出路径长度或不存在路径的标识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

从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 <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#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_queue<Node>q;

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 <<(visited[p->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)
{
    queue<int>q;//
    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 <<endl;

    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值