邻接表+堆优化的dijkstra (最好用的最短路代码)

图结构练习——最短路径

Problem Description

给定一个带权无向图,求节点1到节点n的最短路径。

Input

输入包含多组数据,格式如下。
第一行包括两个整数n m,代表节点个数和边的个数。(n<=100)
剩下m行每行3个正整数a b c,代表节点a和节点b之间有一条边,权值为c。

Output

每组输出占一行,仅输出从1到n的最短路径权值。(保证最短路径存在)

Sample Input

3 2
1 2 1
1 3 1
1 0

Sample Output

1
0

注意:此代码用于稀疏图,或者节点数很多二维数组存不下。在稠密图中可以继续使用邻接矩阵。

代码:

#include <iostream>
#include <stdio.h> 
#include <queue>
#include <cstring>
#define inf 0x3f3f3f

using namespace std;

typedef struct node { // 定义优先队列
    int v,date;
} ty;

bool operator < ( const ty &a, const ty &b )  // 定义优先队列
{
    return a.date>b.date;
}

struct edge {      // 存邻接表的边
    int v,w,next; 
} a[100005];

int head[105];
int dis[105];
int via[105];
int cnt;
int n,m;
ty t,d;

void dijkstra( int v0 ) // 堆优化的dijkstra + 邻接表
{
    priority_queue <ty> Q;
    memset(dis,inf,sizeof(dis));
    dis[v0] = 0;
    t.v = v0;
    t.date = 0;
    Q.push(t);

    while ( !Q.empty() ) {
        if ( via[Q.top().v]==1 ) {
            Q.pop();
            continue ;
        }
        t = Q.top();
        Q.pop();
        int u = t.v;
        dis[u] = t.date;
        via[u] = 1;

        for ( int i=head[u]; i!=-1; i=a[i].next ) {   // 这块代码容易出错,好好看
            int j = a[i].v;                        // 通过邻接表直接找相邻的点
            if ( via[j]==0 && dis[j]>dis[u]+a[i].w ) {
                dis[j] = dis[u]+a[i].w;
                d.v = j;
                d.date = dis[j];
                Q.push(d);
            }
        }
    }

    cout << dis[n] << endl;
}

int main()
{
    while ( cin >> n >> m ) {
        cnt = 0;
        memset(head,-1,sizeof(head));
        memset(via,0,sizeof(via));
        for ( int i=0; i<m; i++ ) {
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            a[cnt].v = y;             // 无向图
            a[cnt].w = z;
            a[cnt].next = head[x];
            head[x] = cnt++;

            a[cnt].v = x;             // 正反存两次
            a[cnt].w = z;
            a[cnt].next = head[y];
            head[y] = cnt ++;
        }
        dijkstra(1);
    }

    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是使用优化的迪杰斯特拉最短路算法的代码实现,代码中使用了一个优先队列来维护待处理的节点。 ``` #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 100005; struct Edge { int to, w; Edge(int t, int weight) : to(t), w(weight) {} }; vector<Edge> G[MAXN]; int dist[MAXN]; bool vis[MAXN]; void dijkstra(int s) { memset(dist, INF, sizeof(dist)); memset(vis, 0, sizeof(vis)); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; dist[s] = 0; q.push({dist[s], s}); while (!q.empty()) { int u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = true; for (auto& e : G[u]) { int v = e.to, w = e.w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; if (!vis[v]) { q.push({dist[v], v}); } } } } } int main() { int n, m, s; scanf("%d%d%d", &n, &m, &s); for (int i = 1; i <= m; ++i) { int u, v, w; scanf("%d%d%d", &u, &v, &w); G[u].emplace_back(v, w); } dijkstra(s); for (int i = 1; i <= n; ++i) { if (dist[i] == INF) { printf("INF "); } else { printf("%d ", dist[i]); } } return 0; } ``` 这段代码实现了优化的迪杰斯特拉最短路算法,可以处理有向图和无向图中单源最短路径问题。 ### 回答2: #include <iostream> #include <vector> #include <queue> #include <limits> using namespace std; struct Edge { int to; int weight; }; // 定义优先队列的比较规则,按照边的权重大小进行排序 struct cmp { bool operator()(const Edge& a, const Edge& b) { return a.weight > b.weight; } }; void dijkstra(vector<vector<Edge>>& graph, int start, vector<int>& dist) { int n = graph.size(); // 初始化距离数组,并将起点的距离设为0 dist.resize(n, numeric_limits<int>::max()); dist[start] = 0; // 优先队列用于选择权重最小的边 priority_queue<Edge, vector<Edge>, cmp> pq; pq.push({start, 0}); while (!pq.empty()) { Edge cur = pq.top(); pq.pop(); int curNode = cur.to; int curWeight = cur.weight; // 如果当前边的权重大于已经找到的最短路径,则忽略该边 if (curWeight > dist[curNode]) { continue; } // 遍历当前节点的所有邻接节点 for (const Edge& next : graph[curNode]) { int nextNode = next.to; int nextWeight = next.weight; // 如果通过当前节点到达邻接节点的路径更短,则更新最短路径 if (curWeight + nextWeight < dist[nextNode]) { dist[nextNode] = curWeight + nextWeight; pq.push({nextNode, dist[nextNode]}); } } } } int main() { int n, m; // n为节点数,m为边数 cin >> n >> m; // 构建邻接表表示的图 vector<vector<Edge>> graph(n); for (int i = 0; i < m; i++) { int u, v, w; // 边的起点、终点和权重 cin >> u >> v >> w; graph[u].push_back({v, w}); } int start, end; // 起点和终点 cin >> start >> end; vector<int> dist; dijkstra(graph, start, dist); if (dist[end] == numeric_limits<int>::max()) { cout << "不存在从起点到终点的路径" << endl; } else { cout << "起点到终点的最短路径长度为:" << dist[end] << endl; } return 0; } ### 回答3: 下面是一个使用优化的迪杰斯特拉算法的最短路代码: ```python import heapq def dijkstra(graph, start): # 初始化距离字典,将起点到每个顶点的距离设为无穷大 distances = {node: float('inf') for node in graph} # 将起点的距离设为0 distances[start] = 0 # 使用来存储当前最小距离的顶点 heap = [(0, start)] while heap: # 弹出中最小距离的顶点 current_distance, current_node = heapq.heappop(heap) # 如果当前顶点距离已经大于之前计算的最小距离,则跳过该顶点 if current_distance > distances[current_node]: continue # 遍历当前顶点的邻居节点 for neighbor, weight in graph[current_node].items(): distance = current_distance + weight # 如果通过当前顶点到达相邻顶点的距离小于之前计算的最小距离,则更新距离并加入 if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(heap, (distance, neighbor)) return distances ``` 这个代码使用了字典来表示图,每个顶点作为键,与其相邻顶点及对应的权重作为值。迪杰斯特拉算法的核心是通过不断更新距离字典中的值来获得最短路径。通过使用来存储当前最小距离的顶点,可以提高算法的效率。算法首先将起点的距离设为0,然后将起点加入中。在每次循环中,弹出中的最小距离顶点,并更新其相邻顶点的最小距离。如果通过当前顶点到达相邻顶点的距离小于之前计算的最小距离,则更新距离,并将相邻顶点加入中。最后,返回最终的距离字典,其中键为顶点,值为起点到该顶点的最小距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值