AcWing850. Dijkstra求最短路 II

问题描述:

给定一个 n n n个点 m m m 条边的有向图,图中可能存在重边和自环,所有边权均为正值。

请你求出 1 1 1 号点到 n n n 号点的最短距离,如果无法从 1 1 1号点走到 n n n 号点,则输出 − 1 −1 1

输入格式:

第一行包含整数 n n n m m m

接下来 m m m 行每行包含三个整数 x x x, y y y, z z z,表示存在一条从点 x x x 到点 y y y 的有向边,边长为 z z z

输出格式:

输出一个整数,表示 1 1 1 号点到 n n n 号点的最短距离。

如果路径不存在,则输出 − 1 −1 1

数据范围

1 ≤ n ≤ 500 , 1≤n≤500, 1n500,
1 ≤ m ≤ 105 , 1≤m≤105, 1m105,
图中涉及边长均不超过 10000 10000 10000

输入样例:

3 3
1 2 2
2 3 1
1 3 4

输出样例:

3

时间复杂度 O ( m l o g n ) O(mlogn) O(mlogn)

C++代码:

堆优化版本Dijkstra

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int N = 150010;

typedef pair<int, int> PII; //堆中存的是pair

int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    
    dist[1] = 0;
    
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1}); // 1号点放入 距离是0 编号为1
    
    while (heap.size()) //堆不空
    {
        auto t = heap.top();
        heap.pop();
        
        int ver = t.second, distance = t.first;//ver为编号 dist为距离
        
        if (st[ver]) continue;//如果用过不用了
        st[ver] = true;
        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > distance + w[i])
            {
                dist[j]  = distance + w[i];
                heap.push({dist[j], j});
            }
        }
    }
    
    if (dist[n] == 0x3f3f3f3f) return -1;
    else return dist[n];
}

int main()
{
    scanf("%d%d", &n, &m);
    
    memset(h, -1, sizeof h); //初始化邻接表
    
    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c); //邻接表存储 对于重边来说 无所谓了。
    }
    
    int t = dijkstra();
    
    cout << t << endl;
    
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dijkstra算法单源最短路径的经典算法,其基本思想是通过逐步扩展生成最短路径集合,最终得到源点到所有其它点的最短路径。 以下是C++实现: ```c++ #include <iostream> #include <vector> #include <queue> #include <cstring> using namespace std; const int INF = 0x3f3f3f3f; // 定义正无穷 struct Edge { int to, w; Edge(int to, int w) : to(to), w(w) {} }; vector<Edge> G[100010]; // 邻接表存图 int dist[100010]; // 存储最短路径长度 bool vis[100010]; // 标记是否已经确定最短路径 void dijkstra(int s) { memset(dist, INF, sizeof(dist)); // 初始化距离为正无穷 memset(vis, false, sizeof(vis)); // 初始化标记为未确定最短路径 dist[s] = 0; // 源点到自己的距离为0 priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; // 小根堆 q.push(make_pair(0, 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; int w = e.w; if(dist[v] > dist[u] + w) { // 如果当前路径更优 dist[v] = dist[u] + w; // 更新最短路径距离 q.push(make_pair(dist[v], v)); // 将该点加入小根堆 } } } } int main() { int n, m, s; cin >> n >> m >> s; for(int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; G[u].push_back(Edge(v, w)); } dijkstra(s); for(int i = 1; i <= n; i++) { if(dist[i] == INF) cout << "INF" << endl; // 如果不连通,输出INF else cout << dist[i] << endl; } return 0; } ``` 输入格式:第一行输入三个整数n,m,s,表示图的点数、边数和源点编号。接下来m行每行三个整数u,v,w,表示一条从u到v的有向边,边权为w。 输出格式:输出n行,每行一个整数,表示源点到每个点的最短路径长度。若不连通,则输出INF。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值